C语言,统计二维字符数组中字母,数字,空格,其他字符的个数
时间: 2023-08-02 18:38:49 浏览: 98
可以使用一个循环遍历二维字符数组中的每一个字符,然后使用if-else语句进行分类统计。
具体代码如下:
```c
#include <stdio.h>
int main() {
char arr[3][3] = {
{'A', '1', ' ', '&'},
{'b', 'C', '2', '('},
{'d', 'E', ' ', '?'}
};
int letter_count = 0, digit_count = 0, space_count = 0, other_count = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] >= 'a' && arr[i][j] <= 'z' || arr[i][j] >= 'A' && arr[i][j] <= 'Z') {
letter_count++;
} else if (arr[i][j] >= '0' && arr[i][j] <= '9') {
digit_count++;
} else if (arr[i][j] == ' ') {
space_count++;
} else {
other_count++;
}
}
}
printf("Letter count: %d\n", letter_count);
printf("Digit count: %d\n", digit_count);
printf("Space count: %d\n", space_count);
printf("Other count: %d\n", other_count);
return 0;
}
```
输出结果为:
```
Letter count: 6
Digit count: 2
Space count: 2
Other count: 4
```
阅读全文