developed a strtok alternative

ant2009 picture ant2009 · Mar 23, 2009 · Viewed 9.4k times · Source

I have developed my own version of strtok. Just to practice the use of pointers.

Can anyone see any limitations with this or anyway I can improve.

void stvstrtok(const char *source, char *dest, const char token) 
{
    /* Search for the token. */
    int i = 0;
    while(*source)
    {
        *dest++ = *source++;
        if(*source == token)
        {
            source++;
        }
    }
    *dest++ = '\0';
    }

int main(void)
{
    char *long_name = "dog,sat ,on ,the,rug,in ,front,of,the,fire";
    char buffer[sizeof(long_name)/sizeof(*long_name)];

    stvstrtok(long_name, buffer, ',');

    printf("buffer: %s\n", buffer);

   getchar();

   return 0;
}

Answer

Brian R. Bondy picture Brian R. Bondy · Mar 23, 2009

A side note: The word 'token' is usually used to describe the parts of the string that are returned. Delimiter is used to describe the thing that separates the tokens. So to make your code more clear you should rename token to delimiter and rename dest to token_dest.

Differences in your function and strtok:

There are several differences between your function and strtok.

  • What your function does is simply remove the token separators
  • You only call your function once to process all parts of the string. With strtok you call it multiple times for each part of the string (subsequent times with NULL as the first param).
  • strtok also destroys the source string, whereas your code uses its own buffer (I think better to use your own buffer as you did).
  • strtok stores the position of the next token after each call where the first parameter is NULL. This position is then used for subsequent calls. This is not thread safe though and your function would be thread safe.
  • strtok can use multiple different delimiters, whereas your code uses just one.

That being said, I will give suggestions for how to make a better function, not a function that is closer to strtok's implementation.

How to improve your function (not emulate strtok):

I think it would be better to make the following changes:

  • Have your function simply return the 'next' token
  • Break out of your loop when you have *source or *source == delimiter
  • Return a pointer to the first character of your source string that contains the next token. This pointer can be used for subsequent calls.