import java.util.*; public class Main { public static void main(String[] args) { int x; String s; Scanner sc=new Scanner(System.in); System.out.println("Enter String :"); s=sc.nextLine(); Stack<Character> st = new Stack<Character>(); //characters to stack until we see a space. for (x = 0; x < s.length(); x++) { if (s.charAt(x) != ' ') { st.push(s.charAt(x)); } else { //When we see a space, we print while (st.empty() == false) { System.out.print(st.pop()); } System.out.print(" "); } } // Since there may not be space after // last word. while (st.empty() == false) { System.out.print(st.pop()); } } }
Enter String : Welcome to MSK Technologies
emocleW ot KSM seigolonhceT