Search code examples
javaarraysfor-loopqueuestack

How would I modify my code to delete chars from the beginning?


So instead of starting from the last index I would like it to do the opposite where it would start removing the start of the string (first index).

public class Sample 
{ 
  public static void main(String[] args) 
  {        
    char y[] = {'L','E','M','O','N','A','D','E'};

    int x = y.length;      
    for (int z = 0; z < len; z++)
    {           
      for(int w = 0; w < len - z; w++)
      {          
        System.out.print(word[w]);             
      }        
    }       
  }   
}

So the output of this code would be:

LEMONADE
LEMONAD
LEMONA
LEMON
LEMO
LEM
LE
L

What I would like is the opposite this time, it would start from the starting index

Example:

LEMONADE
EMONADE
MONADE
ONADE
NADE
ADE
DE
D

Please help me with the code I've provided


Solution

  • Here is the code for your answer. This should work for you.

    public class MyClass {
       public static void main(String[] args) { 
            char y[] = {'L','E','M','O','N','A','D','E'};
            int len = y.length;
            for (int z = 0; z < len; z++){
                for(int w = z; w < len; w++){
                    System.out.print(y[w]);
                }
                System.out.println();
            }   
        }
    }