Problem 48
Reverse Words in a Sentence
Easy · 10 pts · String, Two Pointers
Problem
You are given a sentence made of words separated by spaces. Print the same words in reverse order, separated by single spaces. The letters inside each word stay in their original order; only the order of the words changes. Split the sentence into words, reverse the list of words, and join it back together with single spaces.
Input
A single line containing one or more words of lowercase letters separated by spaces.
Output
Print the words in reverse order separated by single spaces.
Constraints
1 <= number of words <= 10000; 1 <= word length <= 20
Example input
the sky is blue
Example output
blue is sky the
Explanation
The words of "the sky is blue" are the, sky, is and blue. In reverse order they read blue, is, sky, the, so the output is blue is sky the.