Home > Net >  String tokenization in C
String tokenization in C

Time:10-31

I am trying to tokenize a string using strtok in C. Since the string has multiple delimiters, I use the strtok however it fails to tokenize the strings if they have white space.

Example:

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);

What I get is Name: Mustafa Phone: (null) Note: 123456789 after it prints.

Since the name has white space, it ruins everything. It just skips the phone number as you can see and assigns it to the note.

How can I fix that? Is it possible to take the string as a whole after the delimiter for example let's say that string to be tokenized is Name:Mustafa Baki. Can I take Mustafa Baki as a whole after :? Do I need concatenation or something like that?

Thank you.

CodePudding user response:

The problem is that strtok uses the second argument as a set to tokenize on. So the string " /" will tokenize either on space ' ' or on slash '/'. Not the full string.

That means name will be pointing to the single string "Mustafa" while phone points to "Baki" and note1 points to "Phone:123456789".

You should use only the slash "/" in the initial calls to strtok. Then if needed strip trailing spaces in the strings.

  •  Tags:  
  • c
  • Related