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

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

What are Strings?

A string is a sequence of characters. In Java, strings are used to represent text. You can use strings to store messages, names, and other kinds of text data.
Strings are usually represented as a series of characters enclosed in quotation marks. For example, in Java, you can create a string like this:
String s = “Hello World”;

In this Article, we will write a program to Capitalize the first character of each word in a String.

Strings:

We can use the various methods of the String class to manipulate strings.
For example, you can use the length() method to get the length of a string, the substring() method to get a substring of a string, and the toUpperCase() method to convert a string to uppercase. You can also use the + operator or the concat() method to concatenate two strings.

Word Capitalization:

In Java, words can be capitalize using the toUpperCase() Function of String Class. It is function that is used to make a lowercae letter to uppercase letter.

Examples to make first letter of a word capital:

Run
import java.util.*;

public class CapitalizeFirstLetter {
    public static void main(String[] args) {
        // Scanner class for taking input
        Scanner scn = new Scanner(System.in);
        System.out.print("Enter a word: ");
        
        // taking input of a word
        String word = scn.nextLine();
        
        // taking out first letter of the word using substring function
        String firstLetter = word.substring(0, 1);
        
        // capitalizing the first letter using toUpperCase function
        firstLetter = firstLetter.toUpperCase();
        String restOfWord = word.substring(1);
        
        String capitalizedWord = firstLetter + restOfWord;
        System.out.println("Capitalized word: " + capitalizedWord);
    }
}
Output:

Enter a word: lakshit
Capitalized word: Lakshit

This program uses the Scanner class to read in a word from the user. It then uses the substring() method to extract the first letter of the word, capitalizes it using the toUpperCase() method, and concatenates it with the rest of the word. Finally, the program prints the capitalized word.

Example to Capitalize the First letter of Each Word in a Sentence:

Run
import java.util.*;

public class Main{
    public static void main(String[] args) {
        // Scanner class for taking input
        Scanner scn = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        
        // taking input of a sentence
        String sentence = scn.nextLine();
        
        // splliting up words using split function
        String[] words = sentence.split(" ");
        
        for (int i = 0; i < words.length; i++) {
            
            // taking letter individually from sentences
            String firstLetter = words[i].substring(0, 1);
            String restOfWord = words[i].substring(1);
            
            //making first letter uppercase using toUpperCase function
            firstLetter = firstLetter.toUpperCase();
            words[i] = firstLetter + restOfWord;
        }
        
        // joining the words together to make a sentence
        String capitalizedSentence = String.join(" ", words);
        System.out.println("Capitalized sentence: " + capitalizedSentence);
    }
}

Output:

Enter a sentence: my name is lakshit
Capitalized sentence: My Name Is Lakshit

This program uses the Scanner class to read in a sentence from the user. It then uses the split() method to break the sentence up into an array of words. The program iterates through the array, capitalizing the first letter of each word and concatenating it with the rest of the word. Finally, the program uses the join() method to join the words back into a sentence and print the result.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription