编写程序实现程序功能:任意输入3行文字,统计3行文字中,数字字符、空格、英文字母的个数,
时间: 2024-02-15 07:03:26 浏览: 21
以下是实现上述功能的 C 语言代码:
```c
#include <stdio.h>
int main() {
int count_num = 0, count_space = 0, count_letter = 0;
int i, j;
char line[100];
for (i = 0; i < 3; i++) {
printf("请输入第 %d 行文字:", i + 1);
fgets(line, 100, stdin);
for (j = 0; j < (sizeof(line) / sizeof(line[0])); j++) {
if (line[j] >= '0' && line[j] <= '9') {
count_num++;
} else if ((line[j] >= 'a' && line[j] <= 'z') || (line[j] >= 'A' && line[j] <= 'Z')) {
count_letter++;
} else if (line[j] == ' ') {
count_space++;
}
}
}
printf("数字:%d,字母:%d,空格:%d\n", count_num, count_letter, count_space);
return 0;
}
```
该程序首先定义了三个计数器变量 `count_num`、`count_space` 和 `count_letter`,分别用于统计数字字符、空格和英文字母的个数。然后使用 `for` 循环三次,每次输入一行文字并逐字符进行判断,如果是数字字符则将 `count_num` 计数器加 1,如果是英文字母则将 `count_letter` 计数器加 1,如果是空格则将 `count_space` 计数器加 1。最后输出统计结果。