gpt4 book ai didi

C 编程初学者(数组)

转载 作者:行者123 更新时间:2023-11-30 19:46:54 28 4
gpt4 key购买 nike

该问题要求输入一个句子(最多 20 个字符)。如果输入的任何部分包含单词 bad,则将其替换为 ---。例如输入baddog会输出:---dog,再比如输入:bade,输出:---e。

我只能设置阵列。

#include <stdio.h>
main()
{
int c, i;
int counts[20];

for (i = 0; i <= 19; i=i+1)
{
counts[i] = 0;
}

c = getchar();
while (c!= '\n')
{
if ()
}
}

最佳答案

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

int main(){
char sentence[20+1];
char *p;

scanf("%20[^\n]", sentence);
if(NULL!=(p = strstr(sentence, "bad"))){//only once!,,
memcpy(p, "---", 3);
}
printf("%s\n", sentence);
return 0;
}

关于C 编程初学者(数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22910121/

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