函数fun的功能是:分别统计出形参str所指的字符串中的大写字母和小写字母的个数,并传递回主函数输出。例如,若str所指的内容为“BAY23Kill”,其中大写字母数为4,小写字母数为3,则应输出:c0=4,c1=3。 请改正程序中的错误,使它能得出正确的结果。错误在每个/****found***/下面的一句代码中。注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 #include <stdio.h> #include <string.h> void fun(char* str, int *c0, int *c1) { int k;int len=strlen(str); /**********found*****1*****/ c0 = c1 = 0; for (k=0; k < len; k++) { /**********found*****2*****/ if ( (str[k] >= 'A') && (str[k] <= 'Z') ) *c0++; if ( (str[k] >= 'a') && (str[k] <= 'z') ) (*c1)++; } } main() { char str[100]; int c0,c1; printf("input string:"); /**********found*****3*****/ scanf("%c", str); /**********found*****4*****/ fun(str, &c0, c1); printf("c0=%d,c1=%d\n", c0, c1); }
时间: 2023-07-23 13:14:51 浏览: 42
#include <stdio.h>
#include <string.h>
void fun(char* str, int *c0, int *c1) {
int k;
int len=strlen(str); // found 1: 缺少类型修饰符 int
*c0 = *c1 = 0; // found 1: c0 和 c1 均需加上 * 解引用符号
for (k=0; k < len; k++) {
if ( (str[k] >= 'A') && (str[k] <= 'Z') ) (*c0)++; // found 2: c0 需加上 * 解引用符号,条件判断需要加上括号
if ( (str[k] >= 'a') && (str[k] <= 'z') ) (*c1)++; // found 2: 同上
}
}
int main() { // found 4: main函数需要加上返回值类型 int
char str[100];
int c0, c1;
printf("input string:"); // found 3: 输入时应使用 %s 格式化字符
scanf("%s", str); // found 4: 缺少取地址符 &
fun(str, &c0, &c1); // found 4: c1 需要加上取地址符 &
printf("c0=%d,c1=%d\n", c0, c1);
return 0; // found 4: main函数需要加上返回值
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)