Problem
Imagine receiving a secret message where the words are jumbled in reverse order, and your task is to decode it by rearranging them back. In this challenge, you will be given a sentence, and your goal is to reverse the order of its words while maintaining the original form of each word. For example, if the input sentence is 'Hello world from code', the output should be 'code from world Hello'. Let's break it down: First, identify the words in the sentence, which are sequences of characters separated by spaces. Then, rearrange these words in reverse order and join them back into a single string. Consider edge cases such as sentences with multiple spaces, punctuation, or even empty inputs.
- Approach suggestions: Consider using string splitting and joining methods available in your programming language.
- Algorithm/data structure hints: You might find it helpful to use a list or array to store the words temporarily.
- Edge cases to consider: What happens if the sentence is empty or contains only spaces?
- Optimization tips: Focus on keeping your solution clean and readable; efficiency is important but secondary in this easy-level challenge.
Input format
A single line containing a sentence with words separated by spaces.
Output format
A single line with the words in the sentence reversed, separated by a single space.
Constraints
The input sentence will contain only printable ASCII characters and spaces. There will be no leading or trailing spaces. The input will not be empty and will not exceed 1000 characters.
Sample input
Hello World
Sample output
World Hello