Reverse Words in String

Medium

Given a string s, reverse the order of words.

A word is a sequence of non-space characters. Words in the string s will have one space between them. There are no leading or trailing spaces.

Example

s: “yoda is a jedi”

Reversed s: “jedi a is yoda”

Testing

Input Format

The first line contains an integer ‘T’ denoting the number of test cases.

For each test case, the input has one line with the string ‘s’.

Output Format

For each test case, the output has a line with the string ‘s’ which is reversed word-wise.

Sample Input

3
i am solving this problem
good evening everyone
hello

Expected Output

problem this solving am i
everyone evening good
hello

Constraints

1 <= T <= 100
1 <= length of s <= 104
Each character of s is either a lowercase English alphabet or a space.

Editorial Link: Editorial