gpt4 book ai didi

计算 C 语言中的某个特定字母?

转载 作者:行者123 更新时间:2023-11-30 20:40:47 25 4
gpt4 key购买 nike

我必须编写一个程序来计算字母“A”在字符串中使用的次数。所以只需大写字母 A。

到目前为止我已经得到了这个:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {

char s[30];
int amount=0;
printf("Enter a string \n");
gets(s);

//???

printf("\nThe letter A has been used %i times\n", amount);

return 0;
}

最佳答案

因为 gets 会自动为您插入终止 \0,所以很简单:

char s[30];
int amount=0;
int i = 0;
printf("Enter a string \n");
gets(s);

for (; s[i] != '\0'; i++) {
if (s[i] == 'A' || s[i] == 'a') amount++;
}


printf("\nThe letter A has been used %i times\n", amount);

关于计算 C 语言中的某个特定字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20447276/

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