Count Uppercase Letters in C

Here is a simple C program to count the number of uppercase letters in the input string:

#include <stdio.h>

int main() {
    char str[100];
    int i, uppercaseCount;

    printf("请输入一个字符串:");
    gets(str);

    i = 0;
    uppercaseCount = 0;
    while(str[i] != '\0') {
        if(str[i] >= 'A' && str[i] <= 'Z') {
            uppercaseCount++;
        }
        i++;
    }

    printf("大写字母的个数为:%d\n", uppercaseCount);

    return 0;
}

This program asks the user to input a string, then uses a loop to iterate through each character in the string. If the character is an uppercase letter (ASCII value between 65 and 90), the uppercase letter counter is incremented. Finally, the program prints out the counting results.

bannerAds