Reverse string by word using StringTokenizer - Java Language Basics

Java examples for Language Basics:StringTokenizer

Description

Reverse string by word using StringTokenizer

Demo Code

 
import java.util.StringTokenizer;
 
public class Main {
        public static void main(String[] args) {
                String strLine = "Java Reverse string by word example";
                //specify delimiter as " " space
                StringTokenizer st = new StringTokenizer(strLine, " ");
               //w w  w  . j  a v  a 2 s . c om
                String strReversedLine = "";
               
                while(st.hasMoreTokens())
                {
                        strReversedLine = st.nextToken() + " " + strReversedLine;
                }
               
                System.out.println("Reversed string by word is : " + strReversedLine);
        }
}

Result


Related Tutorials