Slicing Strings with strsep()

The strtok() function is the traditional C library routine used to slice up a string. It has its limitations, however, so a more recent function was included in the library on some compilers to replace it: strsep().

Your C compiler may not support strsep(). That’s fine; you can use strtok(), and you should if you want to write portable code. The strsep() function, however, scans for empty strings as well as multiple strings (an array of strings).

The strsep() function is defined in the string.h header file. Here is the man page format:

char * strsep(char **stringp, const char *delim);

The first argument, **stringp, is one of those annoying pointer-pointer things; it’s the single string to search or an array of strings to search. The second argument, *delim, is a string of one or more separator characters, similar to the strtok() function.

Like strtok(), the strsep() function returns a pointer to the first string yanked out of **strngp. Unlike strtok(), however, the function is called with the same arguments over and over until it returns a NULL pointer.

Here is sample code:

#include <stdio.h>
#include <string.h>

int main()
{
    char *string,*found;

    string = strdup("Hello there, peasants!");
    printf("Original string: '%s'\n",string);

    while( (found = strsep(&string," ")) != NULL )
        printf("%s\n",found);

    return(0);
}

Line 8 assigns text to pointer string. This is pretty much the safest way to assign the string, one that won’t generate any compiler warnings. Other methods might work, but also generate warnings or crash the code.

Line 11 contains a while loop with the condition being the results of the strsep() function. As long as the function doesn’t return a NULL, the pointer found references the first chunk of text pulled out of string. The result it output at Line 12.

Here’s sample output:

Original string: 'Hello there, peasants!'
Hello
there,
peasants!

This function can be used as a solution for this month’s Exercise. In fact, it’s a tighter solution than using strtok() because the format for calling strsep() doesn’t change. So the entire thing can be put into a loop:

    /* parse words in the text */
    windex = 0;
    while ( (*(words+windex) = strsep(&string," ")) != NULL)
        windex++;

The while loop’s condition is a bit complex, but it’s effectively fetching each word in string and storing its location in the words array. Click here to view the entire strsep() solution to this month’s Exercise.

Leave a Reply