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

C 字符串是否包含列表中的单词,c,string,substring,C,String,Substring,我有一个字符串和一组关键字。如果字符串包含列表中的一个关键字,我想检查关键字是否是该字符串的唯一元素。如果不是,我想返回一个错误。最后,字符串将始终以\n结尾 我的关键字数组如下所示: const char * keywordsTable[] = { "INIT", "BEGIN", "END", "ROUTINES", "ENDROUTINES", "ENDWHEN", "WHEN", "WHILE" }; 例

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

我的关键字数组如下所示:

const char * keywordsTable[] = 
    {
    "INIT",
    "BEGIN",
    "END",
    "ROUTINES",
    "ENDROUTINES",
    "ENDWHEN",
    "WHEN",
    "WHILE"
    };
例如,如果我的字符串是
“BEGIN\n”
,则一切正常。如果我的字符串是
“BEGIN FOO\n”
“FOO BEGIN\n”
,我必须返回一个错误。最后,如果我的字符串是
“Beginfo\n”
,则一切正常。(错误代码为1,否则为0)

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

int CheckKeyword(char*str)
{
int nKeywords=sizeof(关键字稳定)/sizeof(关键字稳定[0]);
char*strTok=NULL;
char*keywrdWithLF=malloc(20);
//我不想检查最后两个关键字,也不想检查第一个关键字
对于(inti=1;i

提前谢谢你(请不要抱怨我的缩进样式,我必须使用白色缩进)

也许是另一种方法

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);
   }
int CheckKeyword(char*str)
{
int nKeywords=sizeof(关键字稳定)/sizeof(关键字稳定[0]);
char*strTok=NULL;
对于(inti=1;i
好的,这里的问题是什么?我在代码中写下了我被卡住的地方“我被卡住了”不是问题。到底是什么问题?我只是不知道如何继续。。。我将在问题中指出,在任何情况下,它都比strcpy()更安全,并且错误处理得更好。无论如何,这不是我问题的主题。非常感谢,清晰简洁的代码,工作如期!谢谢这段代码,但我忘了输入常量,因为我不能修改str值。因此,对于您的解决方案,我必须制作一个memcpy,而另一个给定的代码更有效(感谢您使用相同的缩进样式):)
   /* 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);
   }
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;
    }