WAP to Reverse Words without Changing Order & Split

Reverse Words without Changing Order & Split: This Java program takes an input string and reverses the order of characters within each word while keeping the words themselves in the same order. The program uses a stack data structure to achieve this.

Reverse Words without Changing Order & Split

package com.softwaretestingo.interviewprograms;
import java.util.Stack;
public class InterviewPrograms27 
{
	/*
	 * Input: reverse me without split 
	 * Output: esrever em tuohtiw tilps
	 */
	public static void main(String[] args) 
	{
		String str = "reverse me without split";
		Stack st=new Stack<Character>();
		for (int i = 0; i < str.length(); ++i) 
		{
			if (str.charAt(i) != ' ')
				st.push(str.charAt(i));
			else {
				while (st.empty() == false) 
				{
					System.out.print(st.pop());
				}
				System.out.print(" ");
			}
		}
		while (st.empty() == false) 
		{
			System.out.print(st.pop());
		}
	}
}

Output

esrever em tuohtiw tilps

Here’s a breakdown of the program:

  1. Import the required Java classes:
    • The program imports the Stack class from the java.util package, which is used to implement the stack data structure.
  2. Define the main method:
    • The entry point of the program is the main method.
  3. Initialize variables:
    • The input string str is initialized with the value “reverse me without split”.
    • A Stack object named st is created to store characters temporarily.
  4. Loop through the input string:
    • The program iterates through each character of the input string using a for loop.
  5. Stack usage:
    • If the current character is not a space, it means it’s a part of a word, so it is pushed onto the stack.
    • If a space is encountered, it means a word has ended, and the program empties the stack by popping and printing each character in reverse order.
    • After printing the reversed word, a space is printed to separate the words.
  6. Print the reversed string:
    • After the loop, there might still be characters left in the stack. The program empties the stack again and prints the remaining characters.

Overall, this program reverses the characters within each word in the input string without using the split method, which splits the string into words based on spaces. Instead, it utilizes a stack to reverse the characters within each word individually while maintaining their original order. The output will be “esrever em tuohtiw tilps”, which is each word of the input string reversed.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment