Count Uppercase Letters

Count number of uppercase letters in a string.

BeginnerTopic: Module 4: String Programs
Back

Java Count Uppercase Letters Program

This program helps you to learn the fundamental structure and syntax of Java programming.

Try This Code
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = sc.nextLine();

        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (Character.isUpperCase(s.charAt(i))) {
                count++;
            }
        }
        System.out.println("Uppercase letters: " + count);
        sc.close();
    }
}
Output
Enter a string: Hello World
Uppercase letters: 2

Understanding Count Uppercase Letters

We use Character.isUpperCase on each character.

Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.

Table of Contents