Reverse string word by word in java (stack/ stringbuilder /example)

  • Given a string in java, reverse the string word by word.
  • Let us look into couple of examples to understand problem statement.
    • Example 1:
      • String inputString = “Girl Proposes Boy”
      • reverse(inputString) should be “Boy Proposes Girl”
    • Example 2:
      • String inputString = “Hell To Heaven”
      • reverse(inputString) should be “Heaven To Hell”

1. Algorithm: Reverse string word by word (java/ stack /stringbuilder)

  1. Reverse String using StringBuilder
    • Split the input string using space delimiter.
    • Start appending the string from end .
    • We will get string reversed word wise.
  2. Reverse String using Stack.
    • Split the input string using space delimiter.
    • Iterate through the string array (result of previous step) & push each string to stack.
    • Pop each string element from stack and concatenate the string.
    • We we get the desired output.
reverse string word stack java example
Fig 1: Reverse String word by word

2. Program: Reverse string word by word (java/stack/stringbuilder /example)

package org.learn;

import java.util.Scanner;
import java.util.Stack;
import java.util.regex.Pattern;

public class ReverseWordsInString {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			System.out.printf("1. Enter string to reverse : ");
			String inputString = scanner.nextLine();
			
			if (inputString == null || inputString.length() == 0) {
				System.out.println("Enter the valid string");
				return;
			}
			
			String reverse = reverseStringWordWise(inputString);
			System.out.printf("2. Reverse string using StringBuilder is : %s", reverse);
			
			reverse = reverseStringWordWise_Stack(inputString);
			System.out.printf("\n3. Reverse string using stack is : %s", reverse);
		}
	}

	private static String reverseStringWordWise(String inputString) {
		String[] arrString = inputString.trim().split(Pattern.quote(" "));
		StringBuilder builder = new StringBuilder();
		int length = arrString.length;

		while (--length >= 0) {
			builder.append(arrString[length]).append(" ");
		}

		return builder.toString();
	}
	
	private static String reverseStringWordWise_Stack(String inputString) {
		String[] arrString = inputString.trim().split(Pattern.quote(" "));
		Stack<String> stack = new Stack<>();
		for(String input : arrString) {
			stack.push(input);
		}
		
		StringBuilder builder = new StringBuilder();
		while( !stack.isEmpty()) {
			builder.append(stack.pop()).append(" ");
		}
		return builder.toString();
	}
}

3. Output: Reverse string word by word (java/stack/stringbuilder /example)

1. Enter string to reverse : Girl Proposes Boy
2. Reverse string using StringBuilder is : Boy Proposes Girl 
3. Reverse string using stack is : Boy Proposes Girl 

1. Enter string to reverse : Hell To Heaven
2. Reverse string using StringBuilder is : Heaven To Hell 
3. Reverse string using stack is : Heaven To Hell 
Scroll to Top