Kodekraftt
/Blog
/Count Uppercase And Lowercase Characters In A String In Java Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Count Uppercase and Lowercase Characters in a String in Java — Definition, Examples and Program

Learn how to count uppercase and lowercase characters in a string in Java using ASCII value comparison without using built-in string methods.

Count Uppercase and Lowercase Characters in a String in Java

Counting uppercase and lowercase characters in a string is a common string manipulation problem that helps understand ASCII values and character-level operations.

In this program, we identify characters based on their ASCII ranges instead of using built-in methods.

Understanding the Problem

Characters are classified as:

  • Uppercase letters: A to Z (ASCII 65–90)
  • Lowercase letters: a to z (ASCII 97–122)

Example 1

Input: JavaProGRam

Output:

  • Uppercase letters: 4
  • Lowercase letters: 7

Example 2

Input: HELLOworld

Output:

  • Uppercase letters: 5
  • Lowercase letters: 5

Logic Explanation

  1. Traverse the string character by character.
  2. Check if character lies between 'A' and 'Z'.
  3. Check if character lies between 'a' and 'z'.
  4. Increment respective counters.

Java Program to Count Uppercase and Lowercase


import java.util.Scanner;

public class CountUpperLower {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String str = sc.nextLine();

        int upper = 0, lower = 0;

        for (int i = 0; i < str.length(); i++) {

            char ch = str.charAt(i);

            if (ch >= 'A' && ch <= 'Z') {
                upper++;
            } else if (ch >= 'a' && ch <= 'z') {
                lower++;
            }
        }

        System.out.println("Uppercase letters: " + upper);
        System.out.println("Lowercase letters: " + lower);
    }
}

Sample Output


Enter a string: JavaProGRam
Uppercase letters: 4
Lowercase letters: 7

Important Notes

  • Digits and special characters are ignored.
  • ASCII comparison avoids extra method calls.
  • Works efficiently for large strings.

Practice Challenges

  1. Also count digits and special characters.
  2. Convert uppercase to lowercase and vice versa.
  3. Ignore spaces while counting characters.

This program strengthens understanding of ASCII values and character-based string traversal in Java.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.