c语言怎样才能输入一行字符,以回车作为结束标志,分别统计出大写字母,小写字母,空格,数字和其他字符?

while循环语句

C代码和运行结果如下:

统计结果正确,望采纳~

附源码:

#include <stdio.h>

int main() {

    char s[100];

    fgets(s, 100, stdin); // 输入一行字符,包括行尾的'\n'

    int i = 0, upper = 0, lower = 0, space = 0, digit = 0, other = 0;

    while (s[i] != '\n') {

        if (s[i] >= 'A' && s[i] <= 'Z')

            upper++;

        else if (s[i] >= 'a' && s[i] <= 'z')

            lower++;

        else if (s[i] == ' ')

            space++;

        else if (s[i] >= '0' && s[i] <= '9')

            digit++;

        else

            other++;

        i++;

    }

    printf("大写字母:%d, 小写字母:%d, 空格:%d, 数字:%d, 其他:%d\n",

        upper, lower, space, digit, other);

    return 0;

}

温馨提示:答案为网友推荐,仅供参考
相似回答