← Practice problem bank

Dec 28, 2025

Reverse Words in a Sentence

Daily Challenge Archive · Open

easyarrays

100 pts · 10000 ms · 128 MB · Practice only

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.

Examples
Input: ' This is fun ' Output: 'fun is This'. Input: '' Output: ''.
Hints
  • 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

Sign in to submit solutions

Run your code against all test cases and get instant feedback.

Sign in