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

问题描述:

是否有一种更方便的方式来检查一个字符是否与以下任何字符相同,而不会如此没有吸引力?将一个字符与一系列字符进行比较

int NormalSearch(char* Line,char* Word) 

' ' “

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

'如果(,和strchr(! “| {} []”,字[J])){}' – pmg 2014-12-09 12:39:33

可以使用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; 
} 

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

或者,也可以使用switch的情况,但不建议。

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

    default: 
     // no match 
     break; 
} 
+0

和......为downvote原因,好吗? – 2014-12-09 13:05:06

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

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

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