How to Reverse String by Word Using StringTokenizer in Java


In this example, we will learn how to reverse String by Word with StringTokenizer in Java.

Source Code

package com.beginner.examples;

import java.util.StringTokenizer;

public class ReverseWords {

	
	public static void main(String[] args) {
	
		String str = "I really like the Java language";
		
		//Create the StringTokenizer object
		StringTokenizer tokens = new StringTokenizer(str, " ");
		
		String strReverse = "";
		
		while(tokens.hasMoreTokens())
		{
			//Append the read words to the end
			 strReverse = tokens.nextToken()+" "+strReverse;
			 
		}
		
		System.out.println(strReverse);

	}

}

Output:

language Java the like really I 

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments