Java Program to Capitalize the first character of each word in a String


A string is a class of 'java.lang' package that stores a series of characters. Those characters are actually String-type objects. We must enclose the value of string within double quotes. Generally, we can represent characters in lowercase and uppercase in Java. And, it is also possible to convert lowercase characters into uppercase. This article aims to discuss a Java program to convert the first character of each word into uppercase in a string.

Java program to Capitalize the first character of each word in a String

Before making a Java program to convert the first lowercase character of a string into uppercase, let's understand the problem statement first with the help of an example −

Instance

Input String

simply easy learning tutorialspoint

Output string

Simply Easy Learning Tutorialspoint

To capitalize the first character of each word in a string, Java provides a built-in method named 'toUpperCase()' that accepts a lowercase character and returns the corresponding uppercase character.

Example 1

The following example demonstrates how we can capitalize the first character of each word in a string.

Approach

  • First, declare and initialize a String. Then, convert that string into a character array using an in-built method named 'toCharArray()'.

  • Next, take a for loop that will run till the size of character array.

  • Inside this for loop, define an if block to check whether the character array contains a space or not. If the compiler encounters a space then, it will convert the next character to uppercase and update the array.

  • Now, we need to convert the character array back to a string.

  • In the end, print the result and exit.

public class Capitalize {
   public static void main(String[] args) {
      String myinput = "simply easy learning tutorialspoint";
      // store each character to a char array
      char[] charAray = myinput.toCharArray();
      System.out.println("Before capitalizing: " + myinput);
      // for loop to capitalize first letter 
      for(int i = 0; i < charAray.length; i++) {
         // capitalizing first letter of first word
         charAray[0] = Character.toUpperCase(charAray[0]);
         // loop to check if there is space between two letters
         if(charAray[i] == ' ') {
            // capitalizing first letter of rest of the word
            charAray[i+1] = Character.toUpperCase(charAray[i+1]);
         }
      }
      // converting the character array to the string
      myinput = String.valueOf(charAray);
      // to print the final result
      System.out.println("After capitalizing the first letter: " + myinput);
   }
}

Output

Before capitalizing: simply easy learning tutorialspoint
After capitalizing the first letter: Simply Easy Learning Tutorialspoint

Example 2

In the following example, we will do the same task with the same logic but with the help of a user-defined method.

public class Capitalize {
   public static void Capital(String myinput) { // user-defined method
      // store each character to a char array
      char[] charAray = myinput.toCharArray();
      // for loop to capitalize first letter 
      for(int i = 0; i < charAray.length; i++) {
         // capitalizing first letter of first word
         charAray[0] = Character.toUpperCase(charAray[0]);
         // loop to check if there is space between two letters
         if(charAray[i] == ' ') {
            // capitalizing first letter of rest of the word
            charAray[i+1] = Character.toUpperCase(charAray[i+1]);
         }
      }
      // converting the character array to the string
      myinput = String.valueOf(charAray);
      // to print the final result
      System.out.println("After capitalizing the first letter: " + myinput); 
   }
   public static void main(String[] args) {
      String myinput = "simply easy learning tutorialspoint";
      System.out.println("Before capitalizing: " + myinput);
      Capital(myinput); // calling the method to capitalize
   }
}

Output

Before capitalizing: simply easy learning tutorialspoint
After capitalizing the first letter: Simply Easy Learning Tutorialspoint

Conclusion

In this article, we have discussed two ways to capitalize the first character of each word in a string. But, one thing that was common in both ways was the built-in method named 'toUpperCase()' that converts a lowercase character to uppercase.

Updated on: 10-Aug-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements