48

Reverse Words In A String

Difficulty: EASY
Contributed By
Anish De
Avg. time to solve
10 min
Success Rate
90%

Problem Statement

You are given a string of length N. You need to reverse the string word by word. There can be multiple spaces between two words and there can be leading or trailing spaces but in the output reversed string you need to put a single space between two words, and your reversed string should not contain leading or trailing spaces.

For example :

If the given input string is "  Welcome to   Coding  Ninjas", then you should return "Ninjas Coding to Welcome" as the reversed string has only a single space between two words and there is no leading or trailing space.
Input Format :
The first line of input contains a single integer T, representing the number of test cases or queries to be run. 
Then the T test cases follow.

The first and only one of each test case contains a string that you need to reverse word by word.
Output Format :
For every test case, print the reversed string such that there should be only one space between two strings and there should not be any trailing space.
Note :
Do not print anything. It has already been taken care of.
Constraints :
1 <= T <= 50
0 <= N <= 10^5

Time Limit: 1 sec
Sample Input 1 :
2
Welcome   to Coding Ninjas
   I am   a    star
Sample Output 1:
Ninjas Coding to Welcome
star a am I
Explanation For Sample Input 1:
In the first test case, you need to reduce multiple spaces between two words to a single space in the reversed string and observe how the multiple spaces, leading and trailing spaces have been removed.

In the second test case, Your reversed string should not contain leading or trailing spaces.
Sample Input 2 :
1
Hello  World!!
Sample Output 2:
World!! Hello
Reset Code
Full screen
copy-code
Console