字符串是否包含列表中的单词

时间:2014-05-11 20:23:53

标签: c string substring

我有一个字符串和一组关键字。如果字符串包含列表中的一个关键字,我想检查关键字是否是此字符串的唯一元素。如果不是,我想返回一个错误。最后,字符串将始终以\ n。

结尾

我的关键字数组如下:

const char * keywordsTable[] = 
    {
    "INIT",
    "BEGIN",
    "END",
    "ROUTINES",
    "ENDROUTINES",
    "ENDWHEN",
    "WHEN",
    "WHILE"
    };

例如,如果我的字符串是"BEGIN\n",一切都很好。如果我的字符串是"BEGIN FOO\n""FOO BEGIN\n",我必须返回错误。最后如果我的字符串是"BEGINFOO\n",一切都很好。 (错误代码为1,否则为0)

我尝试了一些事情(我不知道如何继续):

int CheckKeyword(char * str)    
    {
    int nKeywords = sizeof(keywordsTable) / sizeof(keywordsTable[0]);
    char * strTok = NULL;
    char * keywrdWithLF = malloc(20);
    // I don't want to check for the last two keywords nor the first
    for (int i = 1; i < nKeywords - 2; i++)
        {
        strcpy_s(keywrdWithLF, 20, keywordsTable[i]);
        strcat_s(keywrdWithLF, 20, "\n");
        strTok = strstr(str, keywrdWithLF);
        // If my string contains a keyword
        if (strTok != NULL)
            {
            // If the string contains other characters... and I'm stuck
            if (strcmp(str, keywrdWithLF))
                {
                }
            else
                {
                free(keywrdWithLF);
                return 1;
                }   
            }           
        }
    free(keywrdWithLF);
    return 0;
    }

提前谢谢(请不要抱怨我的缩进风格,我必须使用Whitesmith缩进)!

2 个答案:

答案 0 :(得分:1)

也许是另一种方法?

int CheckKeyword(char * str)    
   {
   int rCode=0;
   int nKeywords = sizeof(keywordsTable) / sizeof(keywordsTable[0]);
   char *keyword;
   char *cp = keywordsTable;

我假设因为str被定义为“char * str”而不是“const char * str”,所以可以修改输入字符串。因此,为什么不从等式中消除'\ n'问题?

   /* Elininate the newline character from the end of the string. */
   if((cp = strchr(str, '\n'))
      *cp = \0;

   // I don't want to check for the last two keywords nor the first.
   nKeywords -= 3;
   ++keyword;

   /* Loop through the keywords. */
   while(nKeywords)
      {
      // "I want to check if the keyword is the only element of this string." 
      // "If it's not, I want to return an error."
      if((cp=strstr(str, keyword))
         {
         /* Check for stuff prior to the keyword. */
         if(cp != str)
            rCode=1;

         /* Check for stuff after the keyword. */
         // Finally if my string is "BEGINFOO\n", everything is fine.
         if(' ' == str[strlen[keyword])
            rCode=1;

         if(strcmp(cp, keyword))
            rCode=1

         break;
         }

      ++keyword;
      --nKeywords;
      }

   return(rCode);
   }

答案 1 :(得分:1)

int CheckKeyword(char * str)    
    {
    int nKeywords = sizeof(keywordsTable) / sizeof(keywordsTable[0]);
    char * strTok = NULL;
    for (int i = 1; i < nKeywords - 2; i++)
        {
        if(NULL!=(strTok = strstr(str, keywordsTable[i])))
            {
            int len = strlen(keywordsTable[i]);
            if(strTok == str)
                {
                if(str[len]==' ' || str[len]=='\t')
                return 1;
                }
            else
                {
                if((strTok[-1]==' ' || strTok[-1]=='\t') && isspace(strTok[len]))//isspace in <ctype.h>
                return 1;
                }
            }
        }
    return 0;
    }