Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Tokenization of strings in C

In C, I'm attempting to tokenize a string using strtok. I use strtok since the string includes several delimiters, however, it fails to tokenize the strings if they contain white space.

String to tokenize: Name:Mustafa Baki /Phone:123456789 /Note:real
        char *name = strtok(line," /");
        char *phone = strtok(NULL, " /");
        char *note1 = strtok(NULL," /");

        //tokenize name
        name = strtok(name, ":");
        name = strtok(NULL, ":");
        //tokenize phone number
        phone = strtok(phone, ":");
        phone = strtok(NULL, ":");

        //tokenize note

        note1 = strtok(note1, ":");
        note1 = strtok(NULL, ":");
        printf("Name: %s Phone: %s Note: %s \n",name,phone,note1);

Mustafa is the name I got. Phone: (null) (null) Take note of 123456789 when it is printed.

Everything is ruined since the name has white space. As you can see, it just skips the phone number and assigns it to the note.

How do I make that right? Is it feasible to take the string as complete as given in this blog after the delimiter, for example, Name:Mustafa Baki, and tokenize it? Can I take Mustafa Baki in its whole after:? Is concatenation or anything required?

Sign In or Register to comment.