java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3942
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use StringTokenizer E-mail
User Rating: / 304
PoorBest 

The following code reverses the order of words in a String. First it breaks the string into the words using StringTokenizer and reverses the order of the words using LIFO property of Stack.

import java.util.*;

public class StringReverseWord {

    private static void doStringReverseWord() {
   
        String a = "Rohit Khariwal Mohit Parnami";
        Stack  stack = new Stack();

        // this statement will break the string into the words which are separated by space.
        StringTokenizer tempStringTokenizer = new StringTokenizer(a);

        // push all the words to the stack one by one
        while (tempStringTokenizer.hasMoreTokens()) {
            stack.push(tempStringTokenizer.nextElement());
        }

        System.out.println("\nOriginal string: " + a);

        System.out.print("Reverse string: ");
 
       // pop the words from the stack
        while(!stack.empty()) {
            System.out.print(stack.pop());
            System.out.print(" ");
        }

        System.out.println("\n");
    }


      public static void main(String[] args) {
        doStringReverseWord();
    }

}

Output Screen:

Original string: Rohit Khariwal Mohit Parnami
Reverse string: Parnami Mohit Khariwal Rohit 

 Related Tips

 
< Prev   Next >
 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.