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
Return:
(string) blue is sky the
Example 2
Input:
s (string) = hello world
Return:
(string) world hello
Example 3
Input:
s (string) = a good example
Return:
(string) example good a

Split the input into real words, discard empty pieces, then join the words from right to left.

Run your code to see the result.