#K47207. Reverse Words in a Sentence
Reverse Words in a Sentence
Reverse Words in a Sentence
You are given a sentence as a string, which may include extra spaces at the beginning, end, or between words. Your task is to reverse the order of the words in the sentence. A word is defined as a contiguous sequence of non-space characters. Extra whitespace should be ignored such that the words are separated by a single space in the output.
For example, given the sentence The quick brown fox jumps over the lazy dog, the correct output is dog lazy the over jumps fox brown quick The.
The problem can be formulated using the following formula: $$\text{Output} = \text{join}(\text{reverse}(\text{split}(\text{Input})))$$
inputFormat
Input is given from standard input (stdin) as a single line representing the sentence to process. The sentence may contain leading, trailing, or multiple intermediate spaces.
outputFormat
Output the sentence with the words in reverse order on a single line to standard output (stdout). Words in the output must be separated by a single space.## sample
The quick brown fox jumps over the lazy dog
dog lazy the over jumps fox brown quick The