C语言 5习题4 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数

5习题4 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数

代码

#include <stdio.h>
int main()
{
       int letter, space, digit, other;
       char ch;
       letter = space = digit = other = 0;
       while ((ch = getchar ()) != '\n')
       {
            if (ch>='a' && ch <= 'z' || ch>='A'&&ch<='Z')
             letter++;
             else if (ch>='0' && ch <='9')
             digit++;
             else if (ch == ' ')
             space++;
             else
             other++;
             }
            printf ("字母:%d\n", letter);
            printf ("空格:%d\n", space);
            printf ("数字:%d\n", digit);
            printf ("其它字符:%d\n", other);
            return 0;
}

输入:I love you!
结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Garuy/article/details/87877170