55. 编辑程序实现,主函数键入一行字符串,被调函数完成统计其中各种类型字符的数量的功能,要求被调函数的输入为数组指针
时间: 2023-04-04 20:00:29 浏览: 72
可以回答这个问题。被调函数可以使用一个数组来记录各种类型字符的数量,遍历输入的字符串,根据字符的 ASCII 码值判断其类型,然后在对应的数组位置上加一。最后返回这个数组即可。以下是示例代码:
```c
#include <stdio.h>
#include <string.h>
void count_chars(char *str, int *counts) {
int i;
for (i = ; i < strlen(str); i++) {
char c = str[i];
if (c >= 'a' && c <= 'z') {
counts[]++;
} else if (c >= 'A' && c <= 'Z') {
counts[1]++;
} else if (c >= '' && c <= '9') {
counts[2]++;
} else {
counts[3]++;
}
}
}
int main() {
char str[100];
int counts[4] = {};
printf("请输入一个字符串:");
scanf("%s", str);
count_chars(str, counts);
printf("小写字母:%d\n", counts[]);
printf("大写字母:%d\n", counts[1]);
printf("数字:%d\n", counts[2]);
printf("其他字符:%d\n", counts[3]);
return ;
}
```