#C10826. Reverse the Order of Words
Reverse the Order of Words
Reverse the Order of Words
Given a sentence, your task is to reverse the order of the words while removing any extra spaces. In other words, split the sentence using whitespace characters, reverse the sequence of words, and print them separated by a single space. This question tests your ability to manipulate strings and arrays.
Note: The input may contain leading or trailing spaces, or multiple spaces between words. Your solution must handle these cases properly.
The problem can be formally described as follows:
Given a non-negative string ( S ), let ( W = [w_1, w_2, \dots, w_n] ) denote the list of words in ( S ) (ignoring extra spaces). You need to output ( S' = w_n ; w_{n-1} ; \dots ; w_1 ).
inputFormat
The input consists of a single line containing a string ( S ) that may include spaces. The string represents the sentence whose words are to be reversed. Input is provided via standard input (stdin).
outputFormat
Output a single line containing the words of the sentence in reverse order, separated by a single space. The output should be written to standard output (stdout).## sample
hello
hello