[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


strtok

Extract tokens within a character array.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


Prototype:
#include <string.h>
char *strtok(char *str, const char *sep);
Parameters:

Parameters for this facility are:

str  
char *  
The string to be separate  
sep  
const char *  
The separator string  

Remarks:

The strtok() function divides a null terminated character array pointed to by str into separate "tokens". The sep argument points to a null terminated character array containing one or more separator characters. The tokens in str are extracted by successive calls to strtok().

Strtok() works by a sequence of calls to the strtok() function. The first call is made with the string to be divided into tokens, as the first argument. Subsequent calls use NULL as the first argument and returns pointers to successive tokens of the separated string.

The first call to strtok() causes it to search for the first character in str that does not occur in sep. If no character other than those in the sep string can be found, strtok() returns a null pointer (NULL). If no characters from the sep string are found it returns a pointer to the original string. Otherwise the function returns a pointer to the beginning of this first token.

Subsequent calls to strtok() are made with a NULL str argument causing it to return pointers to successive tokens in the original str character array. If no further tokens exist, strtok() returns a null pointer.

Both str and sep must be null terminated character arrays.


NOTE

The sep argument can be different for each call to strtok().

WARNING!

strtok() modifies the character array pointed to by str.
Return:

When first called strtok() returns a pointer to the first token in str. If nothing but separator characters are found strtok returns a null pointer.

Subsequent calls to strtok() with a NULL str argument causes strtok() to return a pointer to the next token or return a null pointer (NULL) when no more tokens exist.

Example of strtok() usage.:
#include <string.h>
#include <stdio.h>

int main(void)
{
	static char s[50] = "(ape+bear)*(cat+dog)";
	char *token;
	char *separator = "()+*";
	
	/* first call to strtok() */
	token = strtok(s, separator);
	
	while(token != NULL)
	{
		puts(token);
		token = strtok(NULL, separator);
	}

	return 0;
}

Output:
ape
bear
cat
dog


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000