How to reverse the words from the given string sentence ?

How to reverse the words from the given string sentence ?

Program
String: MSK Technologies
OutPut: KSM seigolonhceT
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());
        }
	}

}

Output:

Enter String : Welcome to MSK Technologies
emocleW ot KSM seigolonhceT





More Questions


19 . How to count the number of words in a given string sentence?
20 . How to reverse the words from the given string sentence?
21 . How to swap two strings without using the third variable?