用 c 语言写上面的题目
时间: 2024-04-15 07:30:24 浏览: 15
好的,以下是使用 C 语言编写的程序,实现了对输入的字符进行分类统计。
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
int digit_cnt = 0, letter_cnt = 0, operator_cnt = 0, escape_cnt = 0;
while (1) {
ch = getchar();
if (ch == '#') break;
if (isdigit(ch)) digit_cnt++;
else if (isalpha(ch)) letter_cnt++;
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') operator_cnt++;
else if (ch == '\n' || ch == '\a' || ch == '\b' || ch == '\t') escape_cnt++;
}
printf("数字个数:%d\n", digit_cnt);
printf("字母个数:%d\n", letter_cnt);
printf("算术运算符:%d\n", operator_cnt);
printf("转义字符:%d\n", escape_cnt);
return 0;
}
```
程序通过 `while` 循环不断读入输入的字符,并根据字符的类型进行分类统计。当读入字符为 `#` 时,退出循环,输出统计结果。其中 `isdigit` 函数可以判断一个字符是否为数字,`isalpha` 函数可以判断一个字符是否为字母。注意,需要包含头文件 `<ctype.h>` 才能使用这些函数。另外,由于 C 语言没有 `cout` 关键字,因此需要使用 `printf` 函数来输出结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)