Capitalize the First Letter of Each Word in a String

This function takes in a string as input and returns the same string with the first letter of each word capitalized.

Problem

Write a function that takes in a string and returns the string with the first letter of each word capitalized.
Example input: "i am a coding pro"
Example output: "I Am A Coding Pro"

Solution

This solution is in JavaScript. Our A.I. can create solutions in multiple languages.
by kayakane
function capitalize(str) {
  let words = str.split(" ");
  let capitalizedWords = [];
  for (let i = 0; i < words.length; i++) {
    let word = words[i];
    let capitalizedWord = word[0].toUpperCase() + word.slice(1);
    capitalizedWords.push(capitalizedWord);
  }
  return capitalizedWords.join(" ");
}

// This solution is optimal because it is O(n) time complexity and O(n) space complexity.
// It is O(n) time complexity because it loops through the words array once.
// It is O(n) space complexity because it creates a new array of capitalized words.

A.I. Evaluation of the Solution

This solution is correct and demonstrates a good understanding of time and space complexity.

Evaluated at: 2022-11-23 10:17:02