Guardian Native C Library Calls Reference Manual (G06.29+, H06.08+, J06.03+)
strtok(3) Guardian Native C Library Calls Reference Manual
EXAMPLES
The following example demonstrates how to split a string into tokens.
#include <string.h>
#include <locale.h>
#include <stdio.h>
#define LENGTH 40
main()
{
char string1[LENGTH], delimiters[LENGTH];
char *pstr ;
int counter;
(void)setlocale(LC_ALL, "");
printf("Enter the string to be searched: ");
if (fgets(string1, LENGTH, stdin) != NULL) {
printf("Enter the delimiter(s): ");
if (fgets(delimiters, LENGTH, stdin) != NULL) {
if ((pstr = strtok(string1, delimiters ))
!= NULL) {
/* pstr points to the first token */
printf("Token 1 is %s\n", pstr);
counter = 2;
while ((pstr = strtok((char *)NULL, delimiters ))
!= NULL) {
printf("Token %d is %s\n", counter, pstr);
counter++;
}
}
}
}
}
RETURN VALUES
Upon successful completion, the strtok() function returns a pointer to the first byte of the parsed
token in the string. When there is no token in the string, a null pointer is returned.
RELATED INFORMATION
Functions: strcat(3).
6−130 Hewlett-Packard Company 527192-018