Previous Page
Next Page


Divides a string into tokens

#include <string.h>
char *strtok ( char * restrict s1 , const char * restrict s2  );

The strtok( ) function isolates tokens in the string addressed by s1 that are delimited by any of the characters contained in the string addressed by s2. The tokens are identified one at a time by successive calls to strtok( ). On calls after the first, the s1 argument is a null pointer.

On the first call, strtok( ) searches in s1 for the first character that does not match any character in s2, behavior that is similar to the strspn( ) function. The first such character found is considered to be the beginning of a token. Then strtok( ) searches further for the first character that does match any of the characters in s2or the null character that terminates the string, whichever comes firstsimilarly to the strcspn( ) function. This is considered to be the delimiter that ends the token. strtok( ) then replaces this ending delimiter with '\0', and returns a pointer to the beginning of the token (or a null pointer if no token was found), while saving an internal, static pointer to the next character after the ending delimiter for use in subsequent strtok( ) calls.

On each subsequent call with a null pointer as the s1 argument, strtok( ) behaves similarly, but starts the search at the character that follows the previous delimiter. You can specify a different set of delimiters in the s2 argument on each call. The locations that strtok( ) reads from using s2 and writes to using s1 on any given call must not overlap.


char *mnemonic, *arg1, *arg2, *comment;
char line[ ] = "    mul eax,[ebp+4]    ; Multiply by y\n";

mnemonic = strtok( line, " \t" );  // First word, between spaces or tabs.
arg1 = strtok( NULL, ",");         // From there to the comma is arg1.
                                   // (Trim off any spaces later.)
arg2 = strtok( NULL, ";\n" );      // From there to a semicolon or line end.
comment = strtok( NULL, "\n\r\v\f" );  // From there to end of line or page.

printf( "Command:      %s\n"
        "1st argument: %s\n"
        "2nd argument: %s\n"
        "Comment:      %s\n\n",
        mnemonic, arg1, arg2, comment );

This sample produces the following output:

Command:      mul
1st argument: eax
2nd argument: [ebp+4]
Comment:       Multiply by y

See Also

strspn( ), strcspn( ), strstr( ), wcstok( )

Previous Page
Next Page