gpt4 book ai didi

c - 打印 strtok_r 使用的 delim

转载 作者:太空宇宙 更新时间:2023-11-03 23:48:26 24 4
gpt4 key购买 nike

例如我有这段文字:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com