Reversing a String by word using StringTokenizer in Java

In this article, we will discuss how to reverse a string by word using StringTokenizer class

Note:

 

Let us move forward to discuss on reversing a String by word using various approaches

Approach 1: Using StringTokenizer class and iterating while-loop

ReversingStringByWordUsingStringTokenizer.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.StringTokenizer;

public class ReversingStringByWordUsingStringTokenizer {

	public static void main(String[] args) {

		// sample string
		String originalStr = "Water Earth Wind Sky Fire";
		String reversedStr = "";

		// create StringTokenizer object
		StringTokenizer st = new StringTokenizer(originalStr, " ");

		// reverse words by iterating through StringTokenizer tokens
		while(st.hasMoreTokens()) {
			reversedStr = st.nextToken() + " " + reversedStr;
		}

		// print to console
		System.out.println("Reverse String using StringToknizer & while-loop:\n");
		System.out.println("Original String : " + originalStr);
		System.out.println("Reversed String : " + reversedStr.trim());
	}
}

Output:

Reverse String using StringToknizer & while-loop:

Original String : Water Earth Wind Sky Fire
Reversed String : Fire Sky Wind Earth Water

 

Approach 2: Using StringTokenizer class and Stack class

ReversingStringByWordUsingStack.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.Stack;
import java.util.StringTokenizer;

public class ReversingStringByWordUsingStack {

	public static void main(String[] args) {

		// sample string
		String originalStr = "Apple Banana Mango Orange Blueberry";
		String reversedStr = "";

		// declare Stack object to store & retrieve tokens
		Stack<String> stack = new Stack<String>();

		// create StringTokenizer object
		StringTokenizer st = new StringTokenizer(originalStr, " ");

		// iterate and store tokens inside Stack
		while(st.hasMoreTokens()) {
			stack.push(st.nextToken());
		}

		// retrieve tokens from Stack by iterating
		while(!stack.empty()) {
			reversedStr = reversedStr + " " + stack.pop();
		}

		// print to console
		System.out.println("Reverse String using StringToknizer & Stack:\n");
		System.out.println("Original String : " + originalStr);
		System.out.println("Reversed String : " + reversedStr.trim());
	}
}

Output:

Reverse String using StringToknizer & Stack:

Original String : Apple Banana Mango Orange Blueberry
Reversed String : Blueberry Orange Mango Banana Apple

 

Approach 3: Using StringTokenizer class and iterating for-loop in reverse order

ReversingStringByWord.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.StringTokenizer;

public class ReversingStringByWord {

	public static void main(String[] args) {

		// sample string
		String originalStr = "USA RSA KSA IND UK GER RUS";
		String reversedStr = "";

		// create StringTokenizer object
		StringTokenizer st = new StringTokenizer(originalStr, " ");

		// reverse words by iterating through StringTokenizer tokens
		for(int token = st.countTokens()-1; token >=0 ; token--) {

			reversedStr = st.nextToken() + " " + reversedStr;
		}

		// print to console
		System.out.println("Reverse String using StringToknizer & for-loop:\n");
		System.out.println("Original String : " + originalStr);
		System.out.println("Reversed String : " + reversedStr.trim());
	}
}

Output:

Reverse String using StringToknizer & for-loop:

Original String : USA RSA KSA IND UK GER RUS
Reversed String : RUS GER UK IND KSA RSA USA

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Happy Coding !!
Happy Learning !!