Jump to: navigation, search

Description

The strtok() function breaks the string s1 into tokens and null-terminates them. Delimiter-Characters at the beginning and end of str are skipped. On each subsequent call delim may change.

Return value

The first/next token if possible, a null-pointer otherwise.

Prototype

Declared in string.h

The C90 prototype is:

char *strtok(char * str, const char * delim);

The C99 prototype adds restrict-qualifiers:

char *strtok(char * restrict str, const char * restrict delim);

Implementation

In standard C, this can be implemented as (adjust prototype for C99):

#include <string.h> /* strspn() strcspn() */
char *strtok(char * str, const char * delim)
{
    static char* p=0;
    if(str)
        p=str;
    else if(!p)
        return 0;
    str=p+strspn(p,delim);
    p=str+strcspn(str,delim);
    if(p==str)
        return p=0;
    p = *p ? *p=0,p+1 : 0;
    return str;
}

Compilable unit, portable C90 in implementation namespace; public domain; past reviewers: none; current reviews: none

References

The C Standard, 7.21.5.8 (C99 numbering)

Personal tools