- Split the input string 's' into an array of words using space as the delimiter, and store them in the 'words' array.
- Create an empty StringBuilder called 'result' to store the reversed sentence.
- Initialize the 'end' variable to the index of the last word in the 'words' array.
- Iterate through the 'words' array in a forward order.
- Inside the loop, check if the current word is not empty.
- If the current word is not empty, insert it at the beginning of the 'result' StringBuilder.
- If the current word is not the last word, add a space character before it to separate words.
- Repeat steps 5-7 for all words in the 'words' array.
- Convert the 'result' StringBuilder to a string using the toString() method.
- Return the reversed sentence as the result.
Example 1:
Input: s = "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: s = " hello world "
Output: "world hello"