#K96052. Reverse Words in a Sentence
Reverse Words in a Sentence
Reverse Words in a Sentence
Given a single line of input containing a sentence, your task is to reverse the order of the words without reversing the individual characters within each word. The input sentence might include extra spaces at the beginning, end, or between words. You need to correctly handle these cases by ensuring that the output contains only one space between words and no leading or trailing spaces.
For example, if the input is hello world this is a test
, the output should be test a is this world hello
.
Mathematically, if a sentence is represented as \(S = w_1\ w_2\ \ldots\ w_n\), where each \(w_i\) is a word, your task is to compute \(S' = w_n\ w_{n-1}\ \ldots\ w_1\).
inputFormat
The input consists of a single line of text representing the sentence. Read this input from standard input (stdin).
outputFormat
Output the sentence with the order of the words reversed, ensuring that words are separated by a single space. Write the result to standard output (stdout).
## samplehello world this is a test
test a is this world hello