Open In App

Java Program to Capitalize the First Letter of Each Word in a String

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Java Programming, we can be able to capitalize the first letter of each word in the given String value by using toUpperCase(), toLowerCase(), and another one is substring() method. These methods are available String class in Java and these methods are useful for our problem.

In this article, we will learn how to capitalize the first letter of each word in a String in Java using different methods.

Approach to capitalize the first letter of each word in a String

To capitalize the first letter of each word in a String, first, we have taken one String value then take the first character from each word in the given string then print the result.

  • First, we have created one Java class.
  • After that, we created one method that is capitalizeWords() which capitalizes the first letter of each word in a String
  • After that, we called this method in the main method and then gave input to the method already taken String value.
  • Finally, print the result.

Program to capitalize the first letter of each word in a String in Java

1. Using split() and substring() methods

Below is the implementation of split() and substring() methods to capitalize the first letter of each word in a String.

Java




// Java Program to capitalize the first letter of each word in a String using split() and substring() methods
import java.util.Arrays;
public class CapitalizeWordsExample {
    public static void main(String[] args) {
        // input string
        String input = "welcome to geeksforgeeks";
  
        // call the capitalizeWords function and store the result
        String result = capitalizeWords(input);
  
        // print the original and modified strings
        System.out.println("Input: " + input);
        System.out.println("Output: " + result);
    }
  
    // function to capitalize the first letter of each word
    public static String capitalizeWords(String input) {
        // split the input string into an array of words
        String[] words = input.split("\\s");
  
        // StringBuilder to store the result
        StringBuilder result = new StringBuilder();
  
        // iterate through each word
        for (String word : words) {
            // capitalize the first letter, append the rest of the word, and add a space
            result.append(Character.toTitleCase(word.charAt(0)))
                  .append(word.substring(1))
                  .append(" ");
        }
  
        // convert StringBuilder to String and trim leading/trailing spaces
        return result.toString().trim();
    }
}


Output

Input welcome to geeksforgeeks
Output Welcome To Geeksforgeeks


Explanation of the Program:

  • In the above program, we have taken one input string value that is welcome to geeksforgeeks.
  • After this we split this String into String array by using split() method.
  • After that we have taken each character from word by using loop from string array.
  • Then change that character from small to capital.
  • Then Append the Result to String builder after that it will print the result.

2. Using Java Streams

Java




// Java Program to capitalize the first letter of each word in a String using Java Streams
import java.util.Arrays;
import java.util.stream.Collectors;
  
public class CapitalizeWordsExample {
    public static void main(String[] args) {
        // input string
        String input = "welcome to geeksforgeeks";
  
        // call the capitalizeWords function and store the result
        String result = capitalizeWords(input);
  
        // print the original and modified strings
        System.out.println("Input: " + input);
        System.out.println("Output: " + result);
    }
  
    // function to capitalize the first letter of each word using Java streams
    public static String capitalizeWords(String input) {
        // split the input string into an array of words, stream the array
        // apply the capitalization transformation and join the words back
        return Arrays.stream(input.split("\\s"))
                     .map(word -> Character.toTitleCase(word.charAt(0)) + word.substring(1))
                     .collect(Collectors.joining(" "));
    }
}


Output

Input: welcome to geeksforgeeks
Output: Welcome To Geeksforgeeks


Explanation of the Program:

  • In the above program, first we have taken a String Value as input.
  • After that converted it into streams by using Array class.
  • Then take the first letter from the word and change into Capital letter then added to the collection.
  • Finally print the result.

3. Using StringBuilder

In this Java code, we have taken one string value, after that we have taken first letter of each word and converted it into capitalize then print the result.

Java




// Java Program to capitalize the first letter of each word in a String using StringBuilder
import java.util.Arrays;
public class CapitalizeWordsExample 
{
    public static String capitalizeWords(String input) 
    {
        if (input == null || input.isEmpty()) 
        {
            return input;
        }
  
        // split the input string into words using whitespace
        String[] words = input.split("\\s");
  
        // create a StringBuilder
        StringBuilder result = new StringBuilder();
  
        for (String word : words) 
        {
            // capitalize the first letter of each word and append the rest of the word
            result.append(Character.toUpperCase(word.charAt(0)))
                  .append(word.substring(1).toLowerCase())
                  .append(" "); // Add a space between words
        }
  
        // remove the trailing space and return the capitalized string
        return result.toString().trim();
    }
  
    public static void main(String[] args) 
    {
      
        String inputString = "welcome to geeksforgeeks";
        String capitalizedString = capitalizeWords(inputString);
  
        System.out.println("Original String: " + inputString);
        System.out.println("Capitalized String: " + capitalizedString);
    }
}


Output

Original String: welcome to geeksforgeeks
Capitalized String: Welcome To Geeksforgeeks


Explanation of the Program:

  • In this above example, first we have taken one string value in main method.
  • After that we have used capitalizeWords().
  • To this method, we have given String value as input.
  • This method can be able to convert capitalize the first letter of each word in a String by using this function.
  • In this user defined function first, we have checked the given string is empty or not, if given String is empty this function returns the empty value as a string.
  • After that we have converted the String into String array by using split method.
  • After that we have created the StringBuilder class object.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads