Update appNew update is available. Click here to update.

Reverse Words in a String II

Last Updated: 20 Mar, 2021
Difficulty: Easy


Try Problem

You are given a string ‘STR’ containing space-separated words. A word is a sequence of non-space characters. Your task is to reverse the order of words in ‘STR’.

Note: Try to do it in-place without allocating extra space.

‘STR’ = “when in doubt use brute force”
The reverse order of words in ‘STR’ is: “force brute use doubt in when”.
1. ‘STR’ does not contain any leading or trailing spaces.
2. The words are always separated by a single whitespace character.
Input format:
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.

The first line and only line of each test case contain a single string ‘STR’.
Output format:
For every test case, return a string with the reverse orders of words as ‘STR’.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 100
1 <= Length of ‘STR’ <= 10^3
The string ‘STR’ contains only ‘a-z’ and whitespace characters.

Time limit: 1 second