#K48992. Reverse Words in a String
Reverse Words in a String
Reverse Words in a String
You are given a string s which may contain leading, trailing, or multiple spaces in between words. Your task is to reverse the order of the words in the string.
For example, if s is " hello world! ", then the reversed string should be "world! hello". The words should be separated by a single space in the output.
Note: If the input string contains only spaces or is empty, the output should be an empty string.
inputFormat
The input consists of a single line containing the string s. The string may have leading, trailing, or multiple spaces between words.
outputFormat
Output the reversed string, where the words are joined by a single space. If the string is empty or contains only spaces, output an empty string.## sample
hello world!
world! hello