Je t’embrasse Salutations from Silicon Valley, California


Python split() in C/C++

I don't know about you, but I rarely want to compress away multiple delimiters in my token-parsing... yet, if you look at the man-page for strtok(), thats exactly what it does:

A sequence of two or more contiguous delimiter bytes in the parsed string is considered to be a single delimiter. Delimiter bytes at the start or end of the string are ignored. Put another way: the tokens returned by strtok() are always nonempty strings.

Seems kinda silly, but that's just how it's been for years and years. So to get around it, I use the following (See accepted answer with regard to Python split() function)

char *strtoks(char *str, const char *delim)
    static char *tok = NULL;
    static char *next = NULL;
    char *m = NULL;

    if (delim == NULL) return NULL;

    tok = (str) ? str : next;
    if (tok == NULL) return NULL;

    m = strstr(tok, delim);
    if (m) {
        next = m + strlen(delim);
        *m = '\0';
    } else {
        next = NULL;

    return tok;
Filed under: C/C++ No Comments