将角色与一系列角色进行比较

时间:2014-12-09 12:37:30

标签: c char

是否有一种更方便的方法来检查一个角色是否与以下任何一个角色相等,而不是没有吸引力呢?

int NormalSearch(char* Line,char* Word)

' ' '

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

3 个答案:

答案 0 :(得分:2)

您可以使用strchr功能:

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

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

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

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

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

答案 1 :(得分:0)

您可以考虑使用strchr()。这是简洁的方法。

或者,也可以使用switch案例,但不建议这样做。

switch (Word[j])
{
    case '|':
    case '{':
    case '}':
    case ']':
    case '[':
    case '.':
        // come out of switch, don't do anything
        break;

    default:
        // no match
        break;
}

答案 2 :(得分:0)

char * t= "|{}[].";

while ( *t && *t != Word[j] ) t++;

if ( *t == 0 )
{
    //
}