gpt4 book ai didi

将一个字符与一系列字符进行比较

转载 作者:太空宇宙 更新时间:2023-11-04 08:33:11 25 4
gpt4 key购买 nike

有没有一种更方便的方法来检查一个字符是否等于以下任何字符,同时又不会那么不吸引人?

int NormalSearch(char* Line,char* Word)

'''

if(Word[j]!='|' && Word[j]!='{' && Word[j]!='}' 
&& Word[j]!='[' && Word[j]!=']' && Word[j]!='.')

最佳答案

您可以使用 strchr功能:

#include <string.h>
...
if (strchr("|{}[].", Word[j]) == NULL) // character not found
...

如果您不能或不能使用 string.h header ,您可以轻松创建自己的版本:

char * my_strchr(char * haystack, char needle)
{
if (!haystack)
return NULL;

while (*haystack && *haystack != needle)
++haystack;

return *haystack || *haystack == needle ? haystack : NULL;
}

关于将一个字符与一系列字符进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27379150/

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