Turn Desktop View Off
by Dinesh Thakur

Stack-This class is a predefined class of java.uti1package. A stack represents a group of elements stored in LIFO (Last In Fast Out) order. This means that the element stored as a last element in the stack will be the first element to be removed from the stack.

Methods of Stack class:

1. bolean empty(): It checks whether the stack is empty or not. If the stack is empty it returns true.

2. element peek():This method returns the top most object from the stack without removing it.

3. element pop():This method remove the top most element from the stack and returns the element.

4. element push(element object): This method insert an element object into the top of the stack and returns that element.

StringTokenizer: This class is a predefined class present in java.util package.It used to break the String into pieces i.e.tokens. These tokens are stored in StringTokenizer object from which they can be retrieved.

Methods:

1. boolean has MoreToken():- It used to test if there are more tokens available in the String Tokenizer object or not. It returns true if next token is present.

2. String nextTokens():- This method returns the next tokens from the StringTokenizer.

3. int countTokens():- It returns the number of tokens present in StringTokenizer object.

Algorithm for Reverse the Order of the Words in a Sentence:

step 1: read String a

step 2: create object of Stack class named sta

step 3: StringTokenizer st= new StringTokenizer(a)

step 4: repeat through step-5 while (st.hasMoreTokens()) returns true

step 5: sta.push( st. nextElement())

step 6: repeat through step-5 while(sta.empty()) returns false

step 7: print sta.pop()

step 8: print one space

step 9: Exit

Here is the Java Example for Reverse the Order of the Words in a Sentence:

import java.util.*;

public class ReverseOrderWords
{
               public static void main(String[] args)
         {
                     System.out.print("Enter a Sentence : ");
                     Scanner s=new Scanner(System.in);
                     String a=s.nextLine();
                     Stack sta=new Stack();
                     StringTokenizer st=new StringTokenizer(a);
                           while(st.hasMoreTokens())
                                   {
                                       sta.push(st.nextElement());
                                   }
                                       System.out.println("\nOriginal string : " + a);
                                       System.out.print("Reverse Order Words : ");
                                             while(!sta.empty())
                                                     {
                                                         System.out.print(sta.pop());
                                                         System.out.print(" ");
                                                     }
                                                        
            }                     
}

Reverse the Order of the Words