编写c语言程序用while循环,输入一行字符,统计这些字符中大写字母、小写字母、数字字符、其他字符的个数。
时间: 2024-06-09 12:11:11 浏览: 123
以下是用while循环编写的c语言程序,可以实现输入一行字符,统计大写字母、小写字母、数字字符和其他字符的个数:
```c
#include <stdio.h>
int main() {
char c;
int upper = 0, lower = 0, digit = 0, other = 0;
printf("请输入一行字符:\n");
while ((c = getchar()) != '\n') {
if (c >= 'A' && c <= 'Z') {
upper++;
} else if (c >= 'a' && c <= 'z') {
lower++;
} else if (c >= '0' && c <= '9') {
digit++;
} else {
other++;
}
}
printf("大写字母:%d\n", upper);
printf("小写字母:%d\n", lower);
printf("数字字符:%d\n", digit);
printf("其他字符:%d\n", other);
return 0;
}
```
程序首先定义了四个变量:`upper`、`lower`、`digit`、`other`,分别用于统计大写字母、小写字母、数字字符和其他字符的个数。
然后程序使用 `getchar()` 函数逐个读入字符,当输入的字符为换行符时停止循环。在每次循环中,根据字符的 ASCII 码值,判断其属于哪一类字符,并将相应计数器加 1。
最后程序输出各类字符的个数。
阅读全文
相关推荐
















