Given a single line of text made up of words separated by one or more spaces (a word itself never contains a space), print the words in reverse order, separated by exactly one space, with no leading or trailing spaces. The line may have leading, trailing, or multiple internal spaces, and may contain no words at all.
Line 1: the text (may be empty or contain only spaces).
The words in reverse order, single-space separated. If there are no words, print an empty line.
Example 1
Input
the quick brown fox
Expected
fox brown quick the
Explanation
The four words reversed are fox, brown, quick, the.
Example 2
Input
hello world
Expected
world hello
Explanation
Extra spaces are collapsed and trimmed; the two words reversed are world, hello.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →