gpt4 book ai didi

c - 搜索字符串并将其与字符串数组进行比较

转载 作者:行者123 更新时间:2023-11-30 17:40:34 26 4
gpt4 key购买 nike

这是我的代码

const char *s[5];={"abcd","efgh","ijkl","mnop","qrst"};
char a[4];
while (1)
{
scanf("%4s",&a);

//compare a with s array

if(/*a exists in s*/)
printf("it is ok");

if(/*a does not exist in s*/)
printf("it is not ok");
}

例如,当我输入“dcba”时,我想看到“it is not ok”。当我输入“efgh”时,我想看到“没关系”。

最佳答案

首先将数组a声明为

char a[5];

使用fgets输入字符串(不要使用 scanf),然后使用 strcmpstrncmp比较两个字符串/(文字)的函数。

关于c - 搜索字符串并将其与字符串数组进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21417439/

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