Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strtok(), no token match

Tags:

c

string

strtok

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

like image 822
mrameshk Avatar asked Nov 29 '12 19:11

mrameshk


1 Answers

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

like image 65
Keith Nicholas Avatar answered Oct 13 '22 21:10

Keith Nicholas