gpt4 book ai didi

C 中的字符数?

转载 作者:行者123 更新时间:2023-12-02 04:43:18 25 4
gpt4 key购买 nike

所以当我编译这段代码时,我得到的字符比我应该多了 1 个,有人请告诉我我正在做的愚蠢错误。

#include <stdio.h>
#include <conio.h>

int main() {
char str[20];
char A = 'a';
char B = 'A';
int count = 0, i;

printf("Enter a string : ");
fgets(str, 20, stdin);

for (i = 0; str[i] != '\0'; i++) {
if (str[i] == A||B)
count++;
}

if (count == 0)
printf("\nCharacter A is not present");
else
printf("\nThere are %d A's in your string.", count);
getchar();

return 0;
}

最佳答案

/* 这就是问题 (str[i] == A)||(str[i] == B) */

#include <stdio.h>
#include <conio.h>

int main() {
char str[20];
char A = 'a';
char B = 'A';
int count = 0, i;

printf("Enter a string : ");
fgets(str, 20, stdin);

for (i = 0; str[i] != '\0'; i++) {
if (str[i] == A || str[i] == B)
count++;
}

if (count == 0)
printf("\nCharacter A is not present");
else
printf("\nThere are %d A's in your string.", count);
getchar();
return 0;
}

关于C 中的字符数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35547186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com