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
shouldn't it be returning NULL if there are no token matches ?
No. The entire string is a token match. – Abbreviatestrchr
. – Rosalbarosalee