strtok() — Tokenize String

Format

#include <string.h>
char *strtok(char *string1, const char *string2);

Language Level: ANSI

Threadsafe: No. Use strtok_r() instead.

Locale Sensitive: The behavior of this function might be affected by the LC_CTYPE category of the current locale. For more information, see Understanding CCSIDs and Locales.

Description

The strtok() function reads string1 as a series of zero or more tokens, and string2 as the set of characters serving as delimiters of the tokens in string1. The tokens in string1 can be separated by one or more of the delimiters from string2. The tokens in string1 can be located by a series of calls to the strtok() function.

In the first call to the strtok() function for a given string1, the strtok() function searches for the first token in string1, skipping over leading delimiters. A pointer to the first token is returned.

When the strtok() function is called with a NULL string1 argument, the next token is read from a stored copy of the last non-null string1 parameter. Each delimiter is replaced by a null character. The set of delimiters can vary from call to call, so string2 can take any value. Note that the initial value of string1 is not preserved after the call to the strtok() function.

Note that the strtok() function writes data into the buffer. The function should be passed to a non-critical buffer containing the string to be tokenized because the buffer will be damaged by the strtok() function.

Return Value

The first time the strtok() function is called, it returns a pointer to the first token in string1. In later calls with the same token string, the strtok() function returns a pointer to the next token in the string. A NULL pointer is returned when there are no more tokens. All tokens are null-ended.

Note:
The strtok() function uses an internal static pointer to point to the next token in the string being tokenized. A reentrant version of the strtok() function, strtok_r(), which does not use any internal static storage, can be used in place of the strtok() function.

Example that uses strtok()

Using a loop, this example gathers tokens, separated by commas, from a string until no tokens are left. The example prints the tokens, a string, of, and tokens.

#include <stdio.h>
#include <string.h>
 
int main(void)
{
   char *token, *string = "a string, of, ,tokens\0,after null terminator";
 
   /* the string pointed to by string is broken up into the tokens
      "a string", " of", " ", and "tokens" ; the null terminator (\0)
      is encountered and execution stops after the token "tokens"     */
   token = strtok(string, ",");
   do
   {
      printf("token: %s\n", token);
   }
   while (token = strtok(NULL, ","));
}
 
/*****************  Output should be similar to:  *****************
 
token: a string
token:  of
token:
token: tokens
*/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]