C Program to Count Uppercase and Lowercase Letters
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
How to Think About It
isupper() or lowercase using islower(). Increase the respective count for each character. Ignore characters that are not letters.Algorithm
Code
#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; }
Dry Run
Let's trace the input "Hello World" through the code
Input string
str = "Hello World"
Initialize counters
uppercase = 0, lowercase = 0
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
Print results
Uppercase letters: 2 Lowercase letters: 8
| Character | Uppercase Count | Lowercase Count |
|---|---|---|
| H | 1 | 0 |
| e | 1 | 1 |
| l | 1 | 2 |
| l | 1 | 3 |
| o | 1 | 4 |
| 1 | 4 | |
| W | 2 | 4 |
| o | 2 | 5 |
| r | 2 | 6 |
| l | 2 | 7 |
| d | 2 | 8 |
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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Using ctype.h functions | O(n) | O(1) | Readability and correctness |
| Using ASCII value checks | O(n) | O(1) | Slightly faster, less readable |
| Using pointers | O(n) | O(1) | Efficient traversal style |
ctype.h functions like isupper() and islower() for clear and reliable letter checks.fgets() which can affect counting.