#K4331. Reverse Words in a String
Reverse Words in a String
Reverse Words in a String
Given a string s
which contains a sequence of words separated by spaces, your task is to reverse the order of the words. The function should remove any extra spaces so that words are separated by a single space.
For example, given the input the sky is blue
, the correct output is blue is sky the
.
Note: The input string can have leading and trailing spaces and multiple spaces between words. You must handle these cases correctly.
Mathematically, if the original order of words is given by \(w_1, w_2, \ldots, w_n\), then the output should be \(w_n, w_{n-1}, \ldots, w_1\).
inputFormat
The input is provided via stdin as a single line string s
, in which words are separated by one or more spaces. The string may contain leading or trailing spaces.
outputFormat
The output should be written to stdout: a single line containing the words from the input string in reversed order, separated by a single space.
## samplethe sky is blue
blue is sky the