cstringtokenizestrsep

strsep() usage and its alternative


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

int main() {

char *slogan = "together{kaliya} [namak]";
char *slow_gun = strdup(slogan);

char *token = strsep(&slow_gun, "{");

printf ("\n slow_gun: %s\n token: %s\n", slow_gun, token);

return 0;
}

when I execute it:

$ cc -o try try_strsep.c
$ ./try

 slow_gun: kaliya} [namak]
 token: together  

But when, I change the char *slogan to:

char *slogan = "kalia} [namak]";

and execute the same program:

$ vi try_strsep.c 
$ cc -o try try_strsep.c
$ ./try

 slow_gun: (null)
 token: kalia} [namak]

My Question is, so when I use strsep() and input string does not have the pattern I am looking for, the return of strsep() is wrong. The only way I can validate whether strsep() could not find the pattern is to check if (slow_gun == NUll).

If I have char *slogan = "together{" then strsep would successfully return token but returns slow_gun to blank (not null)

$ cc -o try try_strsep.c
$ ./try

 slow_gun: 
 token: together

Is there a way I could avoid this IF check and rely on the function to return me the substr and if its not there, return NULL?


Solution

  • No, there's no way to avoid the check slow_gun == NULL. Here's a description of strsep's behavior:

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

    DESCRIPTION
    If *stringp is NULL, the strsep() function returns NULL and does nothing else. Otherwise, this function finds the first token in the string *stringp, where tokens are delimited by symbols in the string delim. This token is terminated by overwriting the delimiter with a null byte ('\0') and *stringp is updated to point past the token. In case no delimiter was found, the token is taken to be the entire string *stringp, and *stringp is made NULL.

    RETURN VALUE
    The strsep() function returns a pointer to the token, that is, it returns the original value of *stringp.

    So, if no match is found strsep returns a pointer to the original string and sets the slow_gun input to NULL.

    If the delimiter is the last character in the string, that character is overwritten by '\0' and slow_gun is set to the following character, which happens to be the '\0' terminating the original string. This is why print statement prints an empty string.

    NOTE You're using strdup incorrectly, the caller is responsible for calling free on the pointer returned by that function.