#C5166. Reverse Words

    ID: 48785 Type: Default 1000ms 256MiB

Reverse Words

Reverse Words

Given a string s, your task is to reverse the order of its words. A word is defined as a sequence of non-space characters. You must remove any leading or trailing spaces and ensure that there is exactly one space separating the words in the output. Formally, if the input string is given as \(s\), you need to compute the output string \(t\) such that:

[ t = w_n ; w_{n-1} ; ... ; w_1 ]

where \(w_i\) are the words in \(s\) after removing extra spaces. For instance, if \(s = " Coding is fun "\), then \(t = "fun is Coding"\).

inputFormat

The input consists of a single line containing the string \(s\). The string may include leading, trailing, and multiple spaces between words.

outputFormat

The output should be a single line string, where the order of words is reversed compared to the input. The output must not have any extra spaces at the beginning or the end, and words should be separated by exactly one space.

## sample
Hello World
World Hello