Home > database >  How to display the Word in a Loop removing the last letter at each iteration
How to display the Word in a Loop removing the last letter at each iteration

Time:05-08

Problem statement:

Create a program that initializes a word in a character array (this becomes your stack) and displays the word removing the last letter on the stack.

Example

Input:

LONELY

Output:

LONELY
LONEL
LONE
LON
LO
L

So this is my code and I have only able to print the character on the array.

But I haven't come up of a solution to keep removing the last letter, just like how the output would show.

And probably need it to be logical.

public static void main(String[] args) {
    char word[] = {'S', 'T', 'U', 'C', 'K'};
    int len = word.length;
        
    for (int x = 0; x < len; x  ) {
        System.out.print(word[x]);
    }
}

CodePudding user response:

You can use nested for loop.

The variable of the inner loop points to a particular character. And the variable defined in the outer loop denotes the number of characters that need to be skipped in the current row.

Statement System.out.println() is executed after each inner loop and advances the output to the next line.

public static void main(String[] args) {
    char word[] = {'L', 'O', 'N', 'E', 'L', 'Y'};
    int len = word.length;
    
    for (int i = 0; i < len; i  ) {
        for (int j = 0; j < len - i; j  ) {
            System.out.print(word[j]); // printing a row
        }
        System.out.println(); // advancing output to the next line
    }
}

But if your challenge implies that you are expected to create an implementation of the stack data structure that will be backed by the character array, that a bit more complicated task.

The code below is meant to provide the general idea on how to it.

There are three important methods that have to be present in the implementation of stack:

  • push() - adds new element (in the method body I've provided the instructions but left out this implementation, so that you're given a chance to obtain a hands-on experience);
  • pop() - removes the element from the top of the stack and returning it;
  • peek() - returns the element from top of the stack without removing it.

Also, I added implementations of isEmpty() and print() methods that could be used to check whether the stack is empty and to print its contents, as their names suggest.

class CharacterStack {
    private char[] letters;
    private int pos = -1;
    
    public CharacterStack(int size) {  // size is passed as an argument
        this.letters = new char[size]; // creating an empty array of characters
    }
    
    public CharacterStack(char[] letters) { // array of characters is passed as an argument
        this.letters = letters;
        this.pos = letters.length - 1; // position at the top of the stack - the greatest valid index of the array
    }
    
    public void push(char letter) {
        /* to do:
          1. check the length of the array
          2. if current position doesn't fit in to it create a new array with greater length
          3. copy the contents of the previous array into a new array
          4. re-assign the variable `letters` to be a newly created array
         */
    }
    
    public char pop() { // removing the element from the top of the stack and returning it
//        return letters[pos--]; a concise equivalent of the lines below
        
        char result = letters[pos];
        pos--;
        return result;
    }
    
    public char peek() { // returning the element from the top of the stack without removing it
        return letters[pos];
    }
    
    public boolean isEmpty() {
        return pos == -1;
    }
    
    public void print() {
        for (int i = 0; i <= pos; i  ) {
            System.out.print(letters[i]);
        }
    }
}

main() - demo

public static void main(String[] args) {
    char[] word = {'L', 'O', 'N', 'E', 'L', 'Y'};
    int len = word.length;
    CharacterStack stack = new CharacterStack(word);
    
    while (!stack.isEmpty()) {
        stack.print();        // printing the contents of the stack

        System.out.println(); // advancing output to the next row
        stack.pop();          // removing the element from the top of the stack
    }
}

Output

LONELY
LONEL
LONE
LON
LO
L
  • Related