我有这段文字,例如:

I know,, more.- today, than yesterday!

我正在用这个代码提取单词:
while(getline(&line, &len, fpSourceFile) > 0) {
  last_word = NULL;
  word = strtok_r(line, delim, &last_word);

  while(word){
    printf("%s ", word);
    word = strtok_r(NULL, delim, &last_word);
    // delim_used = ;
  }
}

输出为:
I know more today than yesterday

但是有什么方法可以得到strtok_r()使用的分隔符我想用一个整数替换相同的单词,并用分隔符替换相同的单词我可以用strtok_r()得到一个单词,但是如何得到该函数使用的分隔符?

最佳答案

幸运的是,strtok_r()是一个非常简单的函数-很容易创建自己的变体来满足您的需要:

#include <string.h>

/*
 * public domain strtok_ex() based on a public domain
 *      strtok_r() by Charlie Gordon
 *
 *   strtok_r from comp.lang.c  9/14/2007
 *
 *      http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
 *
 *     (Declaration that it's public domain):
 *      http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
 */

 /*
    strtok_ex() is an extended version of strtok_r() that optinally
    returns the delimited that was used to terminate the token

    the first 3 parameters are the same as for strtok_r(), the last
    parameter:

        char* delim_found

    is an optional pointer to a character that will get the value of
    the delimiter that was found to terminate the token.

 */
char* strtok_ex(
    char *str,
    const char *delim,
    char **nextp,
    char* delim_found)
{
    char *ret;
    char tmp;

    if (!delim_found) delim_found = &tmp;

    if (str == NULL)
    {
        str = *nextp;
    }

    str += strspn(str, delim);

    if (*str == '\0')
    {
        *delim_found = '\0';
        return NULL;
    }

    ret = str;

    str += strcspn(str, delim);

    *delim_found = *str;
    if (*str)
    {
        *str++ = '\0';
    }

    *nextp = str;

    return ret;
}


#include <stdio.h>
int main(void)
{
    char delim[] = " ,.-!";
    char line[] = "I know,, more.- today, than yesterday!";

    char delim_used;
    char* last_word = NULL;
    char* word = strtok_ex(line, delim, &last_word, &delim_used);

    while (word) {
        printf("word: \"%s\" \tdelim: \'%c\'\n", word, delim_used);
        word = strtok_ex(NULL, delim, &last_word, &delim_used);
    }

    return 0;
}

获取任何跳过的分隔符将需要更多的工作我不认为这会有很多工作,但我确实认为这个界面会很笨拙(strtok_ex()的界面已经很笨重),所以你必须考虑一下。

关于c - 打印strtok_r使用的delim,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27080857/

10-11 04:25