#C9363. Reverse Words in a Sentence
Reverse Words in a Sentence
Reverse Words in a Sentence
You are given a single line of text representing a sentence. Your task is to reverse the order of the words in the sentence while preserving the overall structure. Note that any extra spaces between words or at the beginning or end of the sentence should be removed, and words should be separated by a single space.
For example, if the input is Hello world from Code
, the output should be Code from world Hello
.
This problem can be expressed in terms of a function reverse_words
where the objective is to reverse the order of words. Formally, if a sentence is represented as a sequence of words \(w_1, w_2, \dots, w_n\), then the output should be \(w_n, w_{n-1}, \dots, w_1\).
inputFormat
The input is provided from standard input (stdin) as a single line of text representing the sentence.
outputFormat
Output the sentence with the words in reversed order to standard output (stdout). The words should be separated by a single space, and any leading or trailing spaces should be removed.## sample
Hello world from Code
Code from world Hello