Write a java program to Capitalize the First Letter of Each Word in String.


We can convert the first letter of each word of a string into uppercase by splitting the sentence into words (word array) using split() method and applying toUpperCase() on first character of each word.

But string in java is immutable, how can we Capitalize the First Letter of each Word?

Well, we will declare a new string newString and for each word we will reassign the newString as newString + First letter in upper case + rest of the letters in the word + " ".

By doing so we are not modifying the string but changing the reference to the object.

Let see the source code for clear understanding.

Capitalize the First Letter of Each Word by Splitting Sentence into Words Array


Capitalize the First Letter of Each Word without Splitting Sentence

We can do the above operation without splitting the sentence into an array.

All we need to do is to

  1. Look for the blank space ' ' in the sentence.
  2. If the current character is blank space then concatenate the blank space and the next character after capitalizing it i.e newString = newString + ' ' + Character.toUpperCase(oldString.charAt(i+1)).
  3. Else concatenate the character as it is i.e newString = newString + ch.


If you have any doubts or suggestion then please comment below.

Leave a Reply

eight − one =