DEV Community

Steven Frasica
Steven Frasica

Posted on

Algorithm Problem: Capitalize the First Character

We'll go over another algorithm problem, where we have to capitalize the first character of each word in a string. This solution we'll go over today made the most sense to me, as a problem is always the most clear to me when it can be broken down into small steps.

Problem

We are given a string and are asked to return the string, with the first letter of each word capitalized.

Ex.

capitalize("capitalize every word");
//Output is -> "Capitalize Every Word"

We have to make sure we only capitalize the first letter of the word, and not the entire word. Remember that we have to return a string too.

Approach

We start off with a string data type, and we know we'll be iterating over the data, so it would be useful to convert the string into an array first. We're using the .split() method to convert the string into an array. We'll be splitting up the string into words, not individual characters, so we have to insert a space ' ' as the argument for the split method. Once we have an array of words, we will loop through the array and change each word by capitalizing its first letter.

In order to capitalize, we will use the .toUpperCase() method on each word's first letter. That only gives us the first letter, now we need the rest of the word and we can use the .slice() method for this step. Once we've capitalized the word and rejoined it, we will use the .push() method to add that word to an empty array. The once empty array will contain all of the newly capitalized words, but we're not done yet. We need to convert the array back into a string, and we'll use the .join() method. Make sure to join by a space ' ', similar to how we split it in the first place.

Solution

First, we create an empty array where we will eventually push all the capitalized words.

capitalize(string) {
  const stringArray = []; 
}

Next, we will loop over an array we got from our string.

capitalize(string) {
  const stringArray = [];

  for (let word of string.split(' ')) {
   //We will do work on the array on this line
  }
}

We will take each word and capitalize the first letter of the word. Next, we'll combine it with the rest of the word that we got using the .slice() method. By only passing in the start index as the argument, .slice() will give us the word from the second character to the end of the word.

capitalize(string) {
  const stringArray = [];

  for (let word of string.split(' ')) {
    word[0].toUpperCase() + word.slice(1)
  }
}

The newly capitalized words need somewhere to live, so we will use the .push() method and get them into our currently empty stringArray.

capitalize(string) {
  const stringArray = [];

  for (let word of string.split(' ')) {
    stringArray.push(word[0].toUpperCase() + word.slice(1))
  }

}

Lastly, since we want a string as our answer, we need to return the stringArray in string form. We use .join() to get our newly capitalized string. Make sure to return it at the end and outside of the for loop.

capitalize(string) {
  const stringArray = [];

  for (let word of string.split(' ')) {
    stringArray.push(word[0].toUpperCase() + word.slice(1))
  }
  return stringArray.join(' ');
}

Resources

Stephen Grider's Algorithms and Data Structures Udemy Course

Interview Cake

Top comments (0)