C 统计数字和大写字母和小写字母的个数

image.png

从键盘输入一行字符,分别统计其中大写字母和小写字母的个数

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# include <stdio.h>

int main(void)
{
char str;
int digit,upper,lower;

digit,upper,lower = 0;//初始化为0
while((str = getchar()) != '\n'){
if('0'<=str && str<='9'){
digit+=1;
}else if('a'<=str && str<='z'){
lower+=1;
}else if('A'<=str && str<='Z'){
upper+=1;
}
}

printf("数字类型的个数%d\n", digit);
printf("小写字母的个数%d\n", lower);
printf("大写字母的个数%d\n", upper);

return 0;
}

运行

1
2
[root@izbp1ipfxx237fclphlj7wz c]#  gcc test.c
[root@izbp1ipfxx237fclphlj7wz c]# ./a.out

输入

1
Hello World 666

输出

1
2
3
数字类型的个数3
小写字母的个数8
大写字母的个数2

关联

[[C 计算平均成绩,统计及格人数,计算高于平均分的学生的分数]]

-------------本文结束感谢您的阅读-------------
0%