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