Medium

Reverse Each Word in a Sentence

Description

In this challenge, you are required to write a function that takes a sentence as input and reverses each word in the sentence. A word is defined as a sequence of characters separated by spaces.

This task is commonly used in applications such as text manipulation, data transformation, or encoding algorithms.

To successfully complete the task, your function should handle the following considerations:

Case Sensitivity

  • The function should preserve the original case of each word.

Handling Spaces

  • Words in the sentence are separated by single spaces.
  • Any leading or trailing spaces should not affect the result.

Example Usage

reverseWords("JavaScript is fun"); // ➞ "tpircSavaJ si nuf"
reverseWords(" Reverse leading spaces"); // ➞ " esreveR gnidael secaps"
reverseWords("MULTIPLE words Here"); // ➞ "ELPITLUM sdrow ereH"

Constraints

  • The input will always be a valid string.
  • The function should return a new string, not modify the original.
  • Empty strings should return an empty string.

window code 2Test Cases

Input:

"Hello World"

Expected Output:

"olleH dlroW"