0
0
CProgramBeginner · 2 min read

C Program to Count Uppercase and Lowercase Letters

Use a C program that reads a string and uses if conditions with isupper() and islower() from ctype.h to count uppercase and lowercase letters, like if (isupper(ch)) uppercase++; else if (islower(ch)) lowercase++;.
📋

Examples

InputHello World
OutputUppercase letters: 2 Lowercase letters: 8
InputC PROGRAM
OutputUppercase letters: 9 Lowercase letters: 0
Input1234!@#
OutputUppercase letters: 0 Lowercase letters: 0
🧠

How to Think About It

To count uppercase and lowercase letters, read each character in the input string one by one. Check if the character is uppercase using isupper() or lowercase using islower(). Increase the respective count for each character. Ignore characters that are not letters.
📐

Algorithm

1
Get input string from the user.
2
Initialize two counters: uppercase and lowercase to zero.
3
For each character in the string, check if it is uppercase or lowercase.
4
If uppercase, increment uppercase counter.
5
If lowercase, increment lowercase counter.
6
After processing all characters, print the counts.
💻

Code

c
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main() {
    char str[100];
    int uppercase = 0, lowercase = 0;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove newline character if present
    str[strcspn(str, "\n")] = '\0';

    for (int i = 0; str[i] != '\0'; i++) {
        if (isupper(str[i]))
            uppercase++;
        else if (islower(str[i]))
            lowercase++;
    }

    printf("Uppercase letters: %d\n", uppercase);
    printf("Lowercase letters: %d\n", lowercase);

    return 0;
}
Output
Enter a string: Hello World Uppercase letters: 2 Lowercase letters: 8
🔍

Dry Run

Let's trace the input "Hello World" through the code

1

Input string

str = "Hello World"

2

Initialize counters

uppercase = 0, lowercase = 0

3

Check each character

H (uppercase) -> uppercase=1 e (lowercase) -> lowercase=1 l (lowercase) -> lowercase=2 l (lowercase) -> lowercase=3 o (lowercase) -> lowercase=4 (space) -> ignored W (uppercase) -> uppercase=2 o (lowercase) -> lowercase=5 r (lowercase) -> lowercase=6 l (lowercase) -> lowercase=7 d (lowercase) -> lowercase=8

4

Print results

Uppercase letters: 2 Lowercase letters: 8

CharacterUppercase CountLowercase Count
H10
e11
l12
l13
o14
14
W24
o25
r26
l27
d28
💡

Why This Works

Step 1: Reading input

The program reads a string from the user using fgets() which safely reads a line including spaces.

Step 2: Checking characters

Each character is checked with isupper() and islower() to determine if it is uppercase or lowercase.

Step 3: Counting letters

Counters for uppercase and lowercase letters are incremented accordingly to keep track of the counts.

Step 4: Displaying results

Finally, the program prints the total counts of uppercase and lowercase letters found in the input.

🔄

Alternative Approaches

Using ASCII value checks
c
#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    int uppercase = 0, lowercase = 0;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove newline character if present
    str[strcspn(str, "\n")] = '\0';

    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] >= 'A' && str[i] <= 'Z')
            uppercase++;
        else if (str[i] >= 'a' && str[i] <= 'z')
            lowercase++;
    }

    printf("Uppercase letters: %d\n", uppercase);
    printf("Lowercase letters: %d\n", lowercase);

    return 0;
}
This method uses direct ASCII range checks instead of library functions, which is simple but less readable.
Counting using pointers
c
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main() {
    char str[100];
    int uppercase = 0, lowercase = 0;
    char *p;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove newline character if present
    str[strcspn(str, "\n")] = '\0';

    for (p = str; *p != '\0'; p++) {
        if (isupper(*p))
            uppercase++;
        else if (islower(*p))
            lowercase++;
    }

    printf("Uppercase letters: %d\n", uppercase);
    printf("Lowercase letters: %d\n", lowercase);

    return 0;
}
Using pointers to traverse the string can be more efficient and is a common C style.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through each character once, so the time grows linearly with the input size.

Space Complexity

Only a few counters and the input string are stored, so space usage is constant.

Which Approach is Fastest?

Using ASCII checks is slightly faster but less readable; using ctype.h functions is clearer and preferred for maintainability.

ApproachTimeSpaceBest For
Using ctype.h functionsO(n)O(1)Readability and correctness
Using ASCII value checksO(n)O(1)Slightly faster, less readable
Using pointersO(n)O(1)Efficient traversal style
💡
Use ctype.h functions like isupper() and islower() for clear and reliable letter checks.
⚠️
Beginners often forget to handle newline characters from fgets() which can affect counting.