统计每个数字,空白符以及其他字符出现的次数,要求输出共三行,第一行依次输出0-9出现的次数,空格隔开,第二行输出空白符(空格,制表符,换行符)的个数,第三行输出其他字符出现的次数,写出运行程序的c语言代码
时间: 2024-09-19 20:10:19 浏览: 48
在C语言中,你可以使用`ctype.h`库来处理字符分类,并结合数组和循环结构来统计每个部分的字符出现次数。以下是满足要求的一个简单示例:
```c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char input[1000];
fgets(input, sizeof(input), stdin); // 读取输入
int digit_count[10] = {0}; // 数字(0-9)出现次数
int whitespace_count = 0; // 空白字符(空格、制表符、换行符)
int other_char_count = 0; // 其他字符
for (size_t i = 0; i < strlen(input); ++i) {
if (isdigit(input[i])) {
digit_count[input[i] - '0']++;
} else if (isspace(input[i])) {
whitespace_count++;
} else {
other_char_count++;
}
}
// 输出结果
printf("0-9 count: ");
for (int j = 0; j < 10; ++j) {
if (digit_count[j]) {
printf("%d ", digit_count[j]);
}
}
printf("\n");
printf("Whitespace count: %d\n", whitespace_count);
printf("Other characters count: %d\n", other_char_count);
return 0;
}
```
当你运行这个程序时,它会提示用户输入一行文本,然后统计并打印出指定类型的字符计数值。
阅读全文
相关推荐

















