1

I wanted to test strtok with multiple delimeters, and I wrote the code below, but after printing the first token, token takes the delimeter value instead that of the next word in the string.

#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;
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Nežumi
  • 61
  • 1
  • 7

2 Answers2

4

If your intent is to call strtok in a loop, each time pulling the next token into a string, then change this line:

 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.

to

 token=strtok(NULL, " -.?;,");
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • 1
    ok that was a big mistake by my side, thank you for noticing, i waas going crazy! – Nežumi May 16 '17 at 13:03
  • 1
    @Erida.N - `token=(NULL," -.?;,");` is even more nefarious because it is a completely legal statement syntactically (and therefore may not flag a warning). As is, it might even qualify for an honorable mention in a _obfuscated C_ contest :). – ryyker May 16 '17 at 16:55
2

You dont call strtok again in the loop:

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

this compiles because you use the comma operator here. The NULL expression is discarded and the expression yields " -.?;, and token points to that.

change it to

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

Read more about comma operator here:

What does the comma operator , do?

Community
  • 1
  • 1
Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98