C# program to count upper and lower case characters in a given string

To count uppercase and lowercase characters in a string, we can iterate through each character and check if it falls within specific ASCII ranges. For uppercase letters, we check if the character is between 'A' and 'Z', while for lowercase letters, we check if it's between 'a' and 'z'.

Syntax

Following is the syntax for checking uppercase characters −

if (myStr[i] >= 'A' && myStr[i] 

Following is the syntax for checking lowercase characters −

if (myStr[i] >= 'a' && myStr[i] 

Using ASCII Range Checking

The traditional approach uses ASCII character ranges to identify uppercase and lowercase letters −

using System;

public class Demo {
    public static void Main() {
        string myStr = "Hello World!";
        int upperCase = 0, lowerCase = 0;
        
        Console.WriteLine("String: " + myStr);
        
        for (int i = 0; i = 'a' && myStr[i] = 'A' && myStr[i] 

The output of the above code is −

String: Hello World!
Characters in lowercase: 8
Characters in uppercase: 2

Using Char.IsUpper() and Char.IsLower() Methods

C# provides built-in methods that are more reliable and handle Unicode characters properly −

using System;

public class CharacterCounter {
    public static void Main() {
        string text = "Programming in C# 2024!";
        int upperCount = 0, lowerCount = 0;
        
        Console.WriteLine("Text: " + text);
        
        foreach (char c in text) {
            if (char.IsUpper(c)) {
                upperCount++;
            } else if (char.IsLower(c)) {
                lowerCount++;
            }
        }
        
        Console.WriteLine("Uppercase letters: " + upperCount);
        Console.WriteLine("Lowercase letters: " + lowerCount);
        Console.WriteLine("Total letters: " + (upperCount + lowerCount));
    }
}

The output of the above code is −

Text: Programming in C# 2024!
Uppercase letters: 2
Lowercase letters: 13
Total letters: 15

Using LINQ Count Method

For a more concise approach, you can use LINQ to count characters −

using System;
using System.Linq;

public class LinqExample {
    public static void Main() {
        string input = "TutorialsPoint.COM";
        
        int upperCaseCount = input.Count(char.IsUpper);
        int lowerCaseCount = input.Count(char.IsLower);
        
        Console.WriteLine("Input string: " + input);
        Console.WriteLine("Uppercase count: " + upperCaseCount);
        Console.WriteLine("Lowercase count: " + lowerCaseCount);
    }
}

The output of the above code is −

Input string: TutorialsPoint.COM
Uppercase count: 5
Lowercase count: 9

Comparison of Methods

Method Advantages Disadvantages
ASCII Range Check Fast, simple logic Only works for English letters
Char.IsUpper/IsLower Handles Unicode, reliable Slightly slower than ASCII
LINQ Count Concise, readable code Less efficient for large strings

Conclusion

Counting uppercase and lowercase characters can be accomplished using ASCII range checking, built-in Char methods, or LINQ. The Char.IsUpper() and Char.IsLower() methods are recommended as they properly handle Unicode characters and provide more reliable results than simple ASCII range comparisons.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements