#include <stdio.h>

int main() { char c; int blank = 0, digit = 0, other = 0;

while ((c = getchar()) != '\n')  // 逐个读入字符,直到遇到回车
{
    switch (c)
    {
        case ' ':  // 空格
            blank++;
            break;
        case '0':  // 数字字符
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            digit++;
            break;
        default:  // 其他字符
            other++;
            break;
    }
}

printf("blank = %d, digit = %d, other = %d\n", blank, digit, other);

return 0;

}

用c语言帮我写一个程序题目描述 本题要求编写程序输入一行字符统计其中数字字符、空格和其他字符的个数。建议使用switch语句编写。 输入 输入在一行中给出若干字符最后一个回车表示输入结束不算在内。 输出 在一行内按照 blank = 空格个数 digit = 数字字符个数 other = 其他字符个数

原文地址: https://www.cveoy.top/t/topic/biFB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录