Java Program to Capitalize First Character of Each Word in String

This article covers a program in Java that capitalizes the first character of each and every words in a string. For example, if the string is "we love programming", then the output will be "We Love Programming".

The question is, write a Java program to capitalize first letter of each word in a string. The string must be received by user at run-time of the program. The program given below is answer to this question:

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      String str, firstCharacter, remainingCharacters, capitalizedWord;
      Scanner s = new Scanner(System.in);
      
      System.out.print("Enter the String: ");
      str = s.nextLine();
      
      String words[] = str.split("\\s");
      
      System.out.println("\nThe string is capitalized. The new string is:");
      for(String word: words)
      {
         firstCharacter = word.substring(0, 1);
         firstCharacter = firstCharacter.toUpperCase();
         remainingCharacters = word.substring(1);
         capitalizedWord = firstCharacter + remainingCharacters;
         System.out.print(capitalizedWord + " ");
      }
   }
}

The snapshot given below shows the sample run with user input Hey, welcome to fresherearth as string:

java capitalize each word in string

In above program, the code:

split("\\s")

is used to break the string str into array of words. And the method substring() is used to slice some part of the string based on the index provided as its argument.

That is, the \\s matches the single white space in the string. Therefore, you can replace the above code with:

split(" ")

But I do not recommend to do this, because if there is multiple white spaces available between two words in given string, then the program will produce error. So to avoid errors related to white spaces, I recommend you to use split("\\s+").

In above program, the string entered by user, that is stored in the String variable named str is not actually capitalized, as I only convert first letter of every word of string in capital letter, and then printed that capitalized word, one by one. Which is not a program that we need. Therefore let's modify the above program to create another, that actually capitalizes all words of a string.

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      String str, newstr="", firstchr, remchrs, capwrd;
      Scanner s = new Scanner(System.in);
      
      System.out.print("Enter the String: ");
      str = s.nextLine();
      
      String words[] = str.split("\\s+");
      
      for(String word: words)
      {
         firstchr = word.substring(0, 1);
         firstchr = firstchr.toUpperCase();
         remchrs = word.substring(1);
         newstr = newstr + firstchr + remchrs + " ";
      }
      
      System.out.println("\nThe string \"" +str+ "\" is capitalized.");
      System.out.println("\nThe new string is:");
      System.out.println(newstr);
   }
}

Here is its sample run with user input Hey, welcome to java programming.:

java capitalize first character of each word in string

Java Online Test


« Previous Program Next Program »