所以当我编译这段代码时,我会得到比我应该多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;
}
答案 0 :(得分: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;
}