Step-by-step solution for Letter Capitalize
View the challenge

This challenge requires you to capitalize the first letter of each word. To do this, we'll create an array of words, then loop through each word and capitalize the first letter, and then combine these words back into one string. Below is some simple code to accomplish this:

function LetterCapitalize(str) { 
  
  // split the string into an array
  var words = str.split(" ");

  // we split the word into two parts and then combine the parts 
  // the first part is the first letter which we capitalize and the 
  // second part is the rest of the string
  for (var i = 0; i < words.length; i++) {
    words[i] = words[i].substring(0,1).toUpperCase() + words[i].substring(1);
  }

  // return the array of words joined into a string
  return words.join(" ");
         
}
   
LetterCapitalize("hello world from coderbyte");                                                                          
def LetterCapitalize(str): 

  # split the string into a list
  words = str.split(" ")

  # we split the word into two parts and then combine the parts 
  # the first part is the first letter which we capitalize and the 
  # second part is the rest of the string
  for i in range(0, len(words)): 
    words[i] = words[i][0].upper() + words[i][1:]

  # return the list of words joined into a string
  return " ".join(words)
    
print LetterCapitalize("hello world from coderbyte")             
def LetterCapitalize(str)

  # split the string into an array
  words = str.split(" ")
  
  # we split the word into two parts and then combine the parts 
  # the first part is the first letter which we capitalize and the 
  # second part is the rest of the string
  (0..words.count - 1).each do |i|
     words[i] = words[i][0, 1].upcase + words[i][1..-1]
  end
  
  # return the array of words joined into a string
  return words.join(" ")
         
end
   
puts LetterCapitalize(STDIN.gets)           

There is also another way to solve this challenge by using regular expressions and matching each word in the string and changing the first letter.

function LetterCapitalize(str) { 
  
  // our regex [a-z] matches every alphabetic character in the string
  // but the \b before it specifies a word boundary, in other words, nothing can 
  // come before those letters therefore selecting every word in the string
  return str.replace(/\b[a-z]/gi, function(char) { 
    return char.toUpperCase();
  });
         
}
   
LetterCapitalize("hello world from coderbyte");                                                                          
def LetterCapitalize(str): 

  # in python there is a function called title which is
  # easier than using a regex pattern
  return str.title()
    
print LetterCapitalize("hello world from coderbyte")        
def LetterCapitalize(str): 

  # use regex to match words and then capitalize each one
  return str.gsub(/\w+/) { |w| w.capitalize }
    
print LetterCapitalize("hello world from coderbyte")        
Top User Solutions
1 hyuil Challenge Score: 10 Cpp
1 Qlogin Challenge Score: 10 Python
2 Sabin Challenge Score: 10 Python
1 emuchynski Challenge Score: 10 Ruby
2 junjunparkpark Challenge Score: 10 Ruby
1 cjordanball Challenge Score: 10 JavaScript
3 Corinto Challenge Score: 10 Python
2 EmperorDuny Challenge Score: 10 JavaScript
4 gzamfire Challenge Score: 10 Python
5 mmalkavian Challenge Score: 10 Python
3 GarenLiang Challenge Score: 10 JavaScript
2 lokeshmogra Challenge Score: 10 Cpp
3 cdms0 Challenge Score: 10 Cpp
4 beeboop19 Challenge Score: 10 JavaScript
5 jackgeek Challenge Score: 10 JavaScript
6 hurgadion Challenge Score: 10 Python
7 scates Challenge Score: 10 Python
8 ricky0309 Challenge Score: 10 Python
6 chrigolex Challenge Score: 10 JavaScript
9 philtroin Challenge Score: 10 Python
4 sanguookchoi Challenge Score: 10 Cpp
7 mattlarsh Challenge Score: 10 JavaScript
8 deuzen Challenge Score: 10 JavaScript
1 arjnklc Challenge Score: 10 Java
9 SuprDewd Challenge Score: 10 JavaScript
5 gutty333 Challenge Score: 10 Cpp
10 safetyscissors Challenge Score: 10 JavaScript
10 theBestFella Challenge Score: 10 Python
3 Bohdan Challenge Score: 10 Ruby
4 toasterbob Challenge Score: 10 Ruby
5 duncanbb Challenge Score: 10 Ruby
6 kelaiyarao1 Challenge Score: 10 Ruby
7 mienaineko Challenge Score: 10 Ruby
6 johne Challenge Score: 10 Cpp
8 randyjap Challenge Score: 10 Ruby
9 johncoleman83 Challenge Score: 10 Ruby
1 ionutforyou Challenge Score: 10 PHP
7 Justification Challenge Score: 10 Cpp
10 CoderAPS Challenge Score: 10 Ruby
8 Zeikar Challenge Score: 10 Cpp
2 danaksim Challenge Score: 10 Java
3 jakmaj Challenge Score: 10 Java
4 mikejagen Challenge Score: 10 Java
5 ph4ntom Challenge Score: 10 Java
6 binfeng Challenge Score: 10 Java
9 anta0 Challenge Score: 10 Cpp
10 lingqyzeng Challenge Score: 10 Cpp
7 k8conant Challenge Score: 10 Java
8 nicklivens Challenge Score: 10 Java
9 KSmithson Challenge Score: 10 Java
10 kester824 Challenge Score: 10 Java
2 longcheng Challenge Score: 10 PHP
3 albionselimaj Challenge Score: 10 PHP
4 teplicko Challenge Score: 10 PHP
5 Althalus Challenge Score: 10 PHP
6 smcdowell Challenge Score: 10 PHP
7 jetonr Challenge Score: 10 PHP
8 copacubanana Challenge Score: 10 PHP
9 mihainem Challenge Score: 10 PHP
10 khattab Challenge Score: 10 PHP

Become a Premium Member

With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

“I would like to share that Coderbyte was the major contributor to my studies. I was accepted into Hack Reactor earlier this week, and I wouldn't have gotten this far without it.” ― John Iaconis
  • 200+ Coding Challenges
  • Mock Interview Questions
  • 500,000+ Code Solutions
  • Algorithm Tutorials
  • Interview Prep Courses