Sunil Sharma
Sunil Sharma

Reputation: 21

Compare words using strtok in C

I have started learning c. Currently, I am trying to get words out of two char arrays so that I can compare them using my helper method called compare. However, my strtok() is giving me weird output. Heres my code:

char *headerPointer=headers;
    char *linePointer=firstline;
    printf("Header-%s\n",headers);
    printf("Line-%s\n",firstline);
    headerPointer=strtok(headerPointer,",");
    linePointer=strtok(linePointer,",");
    while ((headerPointer!=NULL&&linePointer!=NULL)) {
            printf("\nPrinting words from headers\n");
            printf("%s",headerPointer);
            headerPointer=strtok(NULL,",");
            printf("\nPrinting words from line\n");
            printf("%s",linePointer);
            linePointer=strtok(NULL, ",");

        }

Output produced by above code:

Header-Hello,My,name,is,Ram.
Line-I,own,20,thousand,bucks.

Printing words from headers-
Hello.
Printing words from line-
I.
Printing words from headers-
own.
Printing words from line-
20.
Printing words from headers-
thousand.
Printing words from line-
bucks.

I don't understand why header is printing the contents from line and where does my,name,is,ram go?

I tried to write them using the following code and this same code produced the desired output. Different Code Style:

char *headerPointer=headers;
    char *linePointer=firstline;
    printf("Header%s\n",headers);
    printf("Line%s\n",firstline);
    headerPointer=strtok(headerPointer,",");

        while(headerPointer!=NULL)
    {
        printf("\nPrinting words from headers\n");
        printf("%s",headerPointer);
        headerPointer=strtok(NULL,",");

    }
    linePointer=strtok(linePointer,",");

    while(linePointer!=NULL){
        printf("\nPrinting words from line\n");
        printf("%s",linePointer);
        linePointer=strtok(NULL, ",");
    }

Output:

Header-Hello,My,name,is,Ram.
Line-I,own,20,thousand,bucks.

Printing words from headers-
Hello.
Printing words from headers-
My.
Printing words from headers-
name.
Printing words from headers-
is.
Printing words from headers-
Ram.
Printing words from line-
I.
Printing words from line-
own.
Printing words from line-
20.
Printing words from line-
thousand.
Printing words from line-
bucks.

Please explain why two codes based on a same idea are producing different results? Can we modify the first code to give results like second? I tried to search and followed the solution already available but couldn't get far.

Upvotes: 0

Views: 306

Answers (1)

ShadowRanger
ShadowRanger

Reputation: 155684

strtok is stateful and non-reentrant; it can only tokenize one string at a time. In your original code, you are trying to tokenize along two inputs at once, but it can't do that; it's only tokenizing along the last non-NULL string argument provided, which was linePointer.

To make this work, you need to use strtok_r which allows you to save the progress on each string without overwriting the progress on the other string.

Upvotes: 4

Related Questions