STRTOK for C guys…


Introduction 

Thanks for your question Ahmet. String tokenizer is one of the most strange functions of C string library. First let me talk about tokenization concept. Assume you are given a string and another fixed delimiter string. Say your string is             “The great grandson of Husnu Sensoy” and the delimiter is happy space character ” “. A token is defined to be any minimum charactered string of the given string between either two delimeter (” “) string or between a delimeter string and string start/end. So the tokens for our example are:

The

great

grandson

of

Husnu

Sensoy

Next question is that how to do this in C ? Here is it:

#include <stdio.h>
#include <string.h> /*Required for strtok function*/

int main() {

    char string[] = “The greatgrandson of Husnu Sensoy”;
    char* token;
    const char dlm[] = ” “;
   
    token = strtok(string,dlm);
   
    while( token != NULL) {
           printf(“%s\n”, token);
           token = strtok(NULL,” “);
    }

    return 0;
}

About kocakahin

Just a computer engineer

Posted on January 13, 2007, in C. Bookmark the permalink. 4 Comments.

  1. Eyvallah hocam, sağolun.

  2. Your function strtok has unknown behavior. Better don’t use it! This function never return NULL in spite of no symbols from second string is in first one!!! At least in gcc 4.5!!! I don’t know some different realization behave as described. This question must be research more carefully! That is very surprise for me. I used that function mane years ago and found its behavior predictable enough and suitable as described. You may use if but criteria for this algorithm must be essential others! For example code like that:
    char *p;
    const char *v = “parameter”;
    p = strtok(v, “=”);
    p must gives NULL, but instead gives the pointer to v!!!
    May be it new standard?

    • You are true that in the first call of strtok, function will return string “parameter”. In the next call of the same function with first parameter as null you will get null as the return value from strtok.

  3. strtok is not thread-safe. strtok_r() is the thread-safe version

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: