在c中标注一个句子的单词(Tokenizing words of a sentence in c)

编程入门 行业动态 更新时间:2024-10-07 08:29:42
在c中标注一个句子的单词(Tokenizing words of a sentence in c)

我试图将一个句子标记为仅由单个空格分隔的单词。 我需要从句子中获取单词,然后将它们写入订阅数组中。 这是我的工作:

void writeToStrArrayOneByOne(char words[10][20], char *sentence){ char *tokenPtr; int j = 0; int a,i; tokenPtr = strtok(sentence," "); while(tokenPtr != NULL){ a = strlen(tokenPtr); for(i=0;i<a;i++){ words[j][i] = tokenPtr[i]; } tokenPtr = strtok(NULL," "); j++; }

我只用这个函数调用这个函数,以便从main调试:

char words[10][20]; char *sentence = "this is a token"; writeToStrArrayOneByOne(words,sentence);

然而,当它是EOF异常时,它会冻结。 感谢任何帮助,谢谢。

I'm trying to tokenize a sentence into words separated only by single space. I need to get words from the sentence and then write them onto subscribed array. Here is my work:

void writeToStrArrayOneByOne(char words[10][20], char *sentence){ char *tokenPtr; int j = 0; int a,i; tokenPtr = strtok(sentence," "); while(tokenPtr != NULL){ a = strlen(tokenPtr); for(i=0;i<a;i++){ words[j][i] = tokenPtr[i]; } tokenPtr = strtok(NULL," "); j++; }

And I call the function with only this in order to debug from main :

char words[10][20]; char *sentence = "this is a token"; writeToStrArrayOneByOne(words,sentence);

However it freezes like when it does when it's EOF exception. Appreciate any help, thanks.

最满意答案

strtok()修改它传递的字符串。 你给它一个不能修改的字符串。

尝试:

char sentence[] = "this is a token";

这将使sentence成为可修改的字符数组。

另外请记住,复制令牌的循环不会在每个条目的末尾放置空终止符 - 我怀疑您可能会需要这些。 也许尝试:

strcpy(words[j], tokenPtr);

其他你应该考虑的事情包括:

处理可能太长的令牌 得到一个带有太多令牌的字符串 如果令牌之间有多个空格,需要做什么 - strtok()不会处理这个问题,如果它对你很重要(我提到这只是因为在你的问题中,你特别提到令牌是“仅由一个空间”)

strtok() modifies the string it's passed. You're giving it a string literal which cannot be modified.

Try:

char sentence[] = "this is a token";

Which will make sentence a modifiable array of characters.

Also keep in mind that the loop where you copy the tokens won't put a null terminator at the end of each entry - I suspect that you'll probably want those. Maybe try:

strcpy(words[j], tokenPtr);

Other things you should think about include:

handling tokens that might be too long getting a string with too many tokens what needs to be done if there is more than one space between tokens - strtok() doesn't deal with that if it matters to you (I mention this only because in your question you specifically mention that tokens are "separated only by a single space")

更多推荐

本文发布于:2023-07-27 04:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:句子   单词   Tokenizing   words   sentence

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!