Closed. This question is off-topic. It is not currently accepting answers. Learn more
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
两年前关闭。
我想用多个delimeters测试strtok,我在下面编写了代码,但是在打印了第一个标记之后,token接受delimeter值,而不是字符串中的下一个单词。
#include <string.h>

int main(int argc, char *argv[]) {
    char sent[]="-This is ?a sentence, with various symbols. I will use strtok to get;;; each word.";
    char *token;
    printf("%s\n",sent);
    token=strtok(sent," -.?;,");
    while(token!=NULL){
        printf("%s\n",token);
        token=(NULL," -.?;,");
    }
    return 0;
}

最佳答案

如果您的意图是在循环中调用strtok,则每次将下一个令牌拉入字符串时,请更改此行:

 token=(NULL," -.?;,");//this syntax results in token being pointed to
                       //each comma separated value within the
                       //parenthesis from left to right one at a time.
                       //The last value, in this case " -.?;,", is what
                       //token finally points to.


 token=strtok(NULL, " -.?;,");

关于c - strtok指针采用分隔符值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44002106/

10-11 22:51
查看更多