题目描述
输入一行字符,以回车符作为输入结束的标志。统计其中英文字母、数字字符和其他字符的个数。
输入
多个字符,以回车符结束,回车符不作为有效字符。有效字符个数不超过100。
输出
输出分3行,格式见输出样例。
样例输入
Abse 4+5*3=?
样例输出
letter:4
digit:3
other:5
思路:
和上一个题大致一样, 不同的是这个题判断条件更多。用循环,判断每一个输入的字符,进而进行计数,需要注意的是满足 :是字母 这个条件的判断语句该如何写。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
char ch = 0; //定义时要定义为字符串类型
int letter = 0;
int digit = 0;
int other = -1; //初始值为-1,其实是减去了一个其他字符中的'\n'
do
{
scanf("%c", &ch);
if (ch >= 'a' && ch <= 'z' || ch <= 'Z' && ch >= 'A') //判定是否为字母
{
letter++;
}
else if (ch <= '9' && ch >= '0') //判定是否为数字
{
digit++;
}
else //非字母数字的其他字符,要注意的是'\n'不能累加,所以初始值为-1,即减去了1
{
other++;
}
} while (ch != '\n'); //输入回车时,结束循环
printf("letter:%d\n", letter);
printf("digit:%d\n", digit);
printf("other:%d", other);
return 0;
}