Intermediate Level
Implement the method reverseWords that reverses the order of words in a string.
Return a string where words appear in reverse order while each word keeps its own character order.
The task is designed to test careful handling of edge cases, not only the most common input.
- Use a single space between words in the output.
- Ignore empty pieces created by repeated spaces.
- Do not reverse characters inside each word.
Example 1
Input
s (string) =
the sky is blueReturns (string)
blue is sky theExample 2
Input
s (string) =
hello worldReturns (string)
world helloExample 3
Input
s (string) =
a good exampleReturns (string)
example good aSplit the input into real words, discard empty pieces, then join the words from right to left.
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run