Capitalize the starting letter of each word in a sentence


Definition

A string can be converted to uppercase using .toUpperCase() method.

Example Program

import java.util.Scanner;

class CapitalizeFirstLetter {

    public static void main(String[] args) {
        System.out.println("Enter the sentence to capitalize the first letter : ");
        Scanner scanner = new Scanner(System.in);
        String string = scanner.nextLine();
        Scanner stringScanner = new Scanner(string);
        String resultString = "";
        while (stringScanner.hasNext()) {
            String str = stringScanner.next();
            resultString = resultString + str.substring(0, 1).toUpperCase() + str.substring(1) + " ";
        }
        System.out.println("After capitalizing the first letter in each word, result is : " + resultString);
    }
}

Sample Output

Enter the sentence to capitalize the first letter :
java is an object-oriented programming language
After capitalizing the first letter in each word, result is: Java Is An Object-Oriented Programming Language