Reverse Words in a String
Reverse Words in a String
Given a sentence as a single line of text, reverse the order of its words. Words are separated by one or more spaces. The result must have words joined by exactly one space with no leading or trailing spaces. Your solve(...) function receives these inputs directly as ready-made arguments — no input reading or parsing needed. Just compute and return the answer. Example: "hello world from flycodx" → "flycodx from world hello"
Input format — only if your code reads from stdin
Input format (stdin): - A single line containing the sentence Output: the words in reversed order, separated by single spaces.
Approach
Split the string on whitespace (this handles multiple spaces naturally), reverse the resulting list, then join with a single space.
Input
Output
- •
sentence length ≤ 10^4 characters
More Strings problems
Browse all coding problems →Press Run to test your custom input, or Submit to judge against all test cases.
Sign in to continue
Create a free account to run code, submit solutions and track progress.
- Free forever — no credit card
- 10+ languages with live execution
- Points, streaks & leaderboards