#K65502. Reverse the Order of Words
Reverse the Order of Words
Reverse the Order of Words
Given a string containing words separated by spaces, your task is to reverse the order of the words.
The output should not contain any extra spaces; that is, there should be no leading or trailing spaces, and words should be separated by a single space.
For example, if the input is " hello world ", the output must be "world hello".
Note: The input string may have multiple spaces between words, or may even be empty. You need to handle all such cases properly.
The problem can be mathematically described as follows: Given a string \( s \), let \( W = [w_1, w_2, \dots, w_n] \) be the sequence of words extracted from \( s \). The required output is the string formed by concatenating \( [w_n, w_{n-1}, \dots, w_1] \) with a single space between each pair of consecutive words.
inputFormat
The input is provided via standard input. It consists of a single line containing a string \( s \). This string may have leading, trailing, or multiple spaces between words.
outputFormat
The output should be written to standard output. It is a single line containing the words from the input in reverse order, separated by a single space, with no extra leading or trailing spaces.
## sample hello world
world hello