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 blue
Returns (string) blue is sky the

Example 2

Input
s (string) = hello world
Returns (string) world hello

Example 3

Input
s (string) = a good example
Returns (string) example good a

Split 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

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.