#K48902. Reverse Words in a String
Reverse Words in a String
Reverse Words in a String
You are given a string s that contains words separated by spaces. Your task is to reverse the order of these words while ensuring that extra spaces at the beginning, end, or between words are handled correctly.
For example, if the input is hello world
, then the correct output is world hello
.
The solution should remove any extra spaces and output the words in a single line separated by a single space.
Note: The reversal should occur on the word level, not on the individual characters.
The problem can also be expressed in the following mathematical style:
For a given string \(s = w_1\ w_2\ \dots\ w_n\), produce an output string \(s' = w_n\ w_{n-1}\ \dots\ w_1\).
inputFormat
The input is provided through standard input (stdin) and consists of a single line string s that may include leading, trailing, or multiple spaces between words.
outputFormat
Output the reversed string to standard output (stdout) with words separated by a single space. There should be no leading or trailing spaces in the output.
## sample hello world
world hello
</p>