我正在尝试编写一个程序来告诉我“昨天,我所有的麻烦似乎都如此遥远”这句话中有多少个“e”字母。我正在尝试使用 strcmp 函数来执行此操作,并且我想将短语中的每个字母与字母“e”进行比较,然后使其计数增加。
int main(void) {
int i, x, count = 0;
char words[] = "Yesterday, all my troubles seemed so far away", letter;
char str1 = "e";
for (i = 0; i < 45; i++) {
letter = words[i];
x = strcmp(letter, str1);
if (x == 0) {
count++;
}
}
printf("The number of times %c appears is %d.\n", str1, count);
}