Easy

Capitalize Each Word

Description

In this challenge, you are required to write a function that takes a sentence as input and capitalizes the first letter of each word.

A word is defined as a sequence of characters separated by spaces.

This task is commonly used in text formatting applications, such as title casing headings, improving readability, or formatting user inputs.

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

Case Sensitivity

  • The function should capitalize only the first letter of each word, leaving the rest unchanged.

Handling Spaces

  • Words are separated by single spaces.
  • The input may contain leading or trailing spaces, which should not affect the result.

Example Usage

capitalizeWords("javaScript is fun"); // ➞ "JavaScript Is Fun"
capitalizeWords(" leading spaces"); // ➞ " Leading Spaces"
capitalizeWords("MULTIPLE WORDS"); // ➞ "MULTIPLE WORDS"

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:

"Hello World"