I was trying to parse strings using strtok()
; I am trying to parse strings delimited by a semicolon ( ; ). But when I input a string with no semicolons to strtok()
, it returns the entire string. Shouldn't it be returning NULL
if there are no token matches?
This is my code:
int main(int argc, char** argv)
{
char cmd[] = "INSERT A->B B->C INSERT C->D";
char delim[] = ";";
char *result = NULL;
result = strtok(cmd,delim);
if(result == NULL)
{
printf("\n NO TOKENS\n");
}
else
{
printf("\nWe got something !! %s ",result);
}
return (EXIT_SUCCESS);
}
The output is : We got something !! INSERT A->B B->C INSERT C->D
No, the delimiter means that it's the thing that separates tokens, so if there is no delimiters, then the entire string is considered the first token
consider if you have two tokens, then take one of those tokens away. if you have
a;b
then you have tokens a and b
now if you take b
away...
a
you still have token a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With