Reverse Words in a String
mediumStrings+20 XPTime limit 2s
Reverse the order of the words in a sentence. Words are separated by one or more spaces, and the line may have spaces at the start or end. Print the words in reverse order separated by single spaces, with no leading or trailing spaces.
Input: one line s (letters, digits and spaces; it contains at least one word).
Output: the words in reverse order.
Constraints: 1 ≤ length of s ≤ 10⁵.
Watch out: read the whole line (including its spaces) — reading word-by-word works too, as long as you don't rely on single spaces. Try it in place: reverse the whole string, then reverse each word.
Sample input 1
the sky is blue
Sample output 1
blue is sky the
Sample input 2
hello world
Sample output 2
world hello
📖 Stuck? Learn the pattern
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).