所以我有一个像这样的字符串:

char* line="foo bar\tbaz\nbiz";


我想一次解析一个字符串一个单词,根据单词来做不同的事情。像这样:

void process_word(const char* str) //str will point to the beginning of a word
{
  if(!strcmp(str, "foo")){
    foo();
  }else if(!strcmp(str, "bar")){
    bar();
  }else{
    others();
  }
}


我该如何做才能使用strcmp之类的东西,而不是在\ 0处停止比较,而在空格或其他空白字符处停止比较呢?

没有新缓冲区创建或正则表达式之类的过大解决方案的奖励积分

最佳答案

这将只检查模式,而不考虑单词终止字符

int mystrncmp(const char *line, const char *pattern)
{
   int len = strlen(pattern);
   return strncmp(line,pattern,len);
}

10-07 13:30
查看更多