Question

Thumbs up for help.create a stack and write a program that reverses the letters in a...

Thumbs up for help.create a stack and write a program that reverses the letters in a string. Then display. use the LinkedList class in java to implement a stack.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

class Stack1

{

    LinkedList<Character> l;

    // constructor

    Stack1()

    {

        // create a linked list

        this.l = new LinkedList<Character>();

    }

   

    // return the size of the Stack1

    public int size()

    {

        return l.size();

    }

   

    // push element into Stack1

    public void push(char ch)

    {

        // addFirst() function adds the element in the front of the list

        this.l.addFirst(ch);

    }

   

    // pop element from Stack1

    public char pop()

    {

        // if Stack1 is empty

        if( this.isEmpty() == true )

        {

            System.out.println("Stack1 is empty");

            return ' ';

        }

       

        // get first element in the list

        char ans = this.l.get(0);

       

        // removeFirst() function removes the element from the front of the list

        this.l.removeFirst();

       

        return ans;

    }

   

    public boolean isEmpty()

    {

        return this.size() == 0;

    }

}

class Solution

{

    // return the reversed string of str

    public static String reverse(String str)

    {

        // create a Stack1 to store character

        Stack1 st = new Stack1();

       

        int i;

       

        // traverse the string

        for( i = 0 ; i < str.length() ; i++ )

        {

            // add the character at index i to Stack1

            // charAt(i) return the character at index i in String

            st.push( str.charAt(i) );

        }

       

        // store the reversed String

        String ans = "";

       

        // loop untill Stack1 is empty

        while( !st.isEmpty() )

            // pop the top most element from Stack1 using pop()

            ans += st.pop();

           

        return ans;

    }

   

    public static void main(String[] args)

    {

        // create a Scanner class object

        Scanner sc = new Scanner(System.in);

       

        System.out.print("Enter a sentence : ");

       

        // read complete line

        String str = sc.nextLine();

       

        // split the string using space as delimeter

        String[] arr = str.split(" ");

       

        int i;

       

        // create an array to store reversed words

        String[] rev = new String[ arr.length ];

       

        for( i = 0 ; i < arr.length ; i++ )

            // reverse the current word

            rev[i] = reverse( arr[i] );

           

        System.out.print("\nReversed sentence : ");

           

        for( i = 0 ; i < rev.length ; i++ )

            System.out.print(rev[i] + " ");

    }

}


Sample Output

Add a comment
Know the answer?
Add Answer to:
Thumbs up for help.create a stack and write a program that reverses the letters in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT