COHERENT manpages

This page displays the COHERENT manpage for strtok() [Break a string into tokens].

List of available manpages
Index


strtok() -- String Function (libc)

Break a string into tokens
#include <string.h>
char *strtok(string1, string2)
char *string1, *string2;

strtok() divides  a string  into a  set of tokens.   string1 points  to the
string to  be divided,  and string2 points  to the character  or characters
that delimit the tokens.

strtok() divides a string into tokens by being called repeatedly.

On the  first call to  strtok(), string1 should  point to the  string being
divided.  strtok()  searches for  a character  that is not  included within
string2. If it finds one, then  strtok() regards it as the beginning of the
first  token within  the string.   If  one cannot  be found,  then strtok()
returns NULL  to signal that the  string could not be  divided into tokens.
When it finds  the beginning of the first token,  strtok() then looks for a
character  that is  included within  string2. When  it finds  one, strtok()
replaces it with  NUL to mark the end of  the first token, stores a pointer
to the remainder of string1 within a static buffer, and returns the address
of the beginning of the first token.

On subsequent calls to strtok(),  pass it NULL instead of string1. strtok()
then looks for  subsequent tokens using the address that  it saved from the
first time you called it.

Note that  with each  call to  strtok(), string2 may  point to  a different
delimiter or set of delimiters.

Example

The following example breaks command_string into individual tokens and puts
pointers  to the  tokens into  the array tokenlist[].  It then  returns the
number of  tokens created.  No  more than maxtoken tokens  will be created.
command_string is modified to  place `\0' over token separators.  The token
list  points into  command_string. Tokens  are  separated by  spaces, tabs,
commas, semicolons, and newlines.

#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>

tokenize(command_string, tokenlist, maxtoken)
char *command_string, *tokenlist[]; size_t maxtoken;
{
    static char tokensep[]="\t\n ,;";
    int tokencount;
    char *thistoken;

    if(command_string == NULL || !maxtoken)
        return 0;

    thistoken = strtok(command_string, tokensep);

    for(tokencount = 0; tokencount < maxtoken &&
            thistoken != NULL;) {
        tokenlist[tokencount++] = thistoken;
        thistoken = strtok(NULL, tokensep);
    }

    tokenlist[tokencount] = NULL;
    return tokencount;
}

#define MAXTOKEN 100
char *tokens[MAXTOKEN];
char buf[80];

main()
{
    for(;;) {
        int i, j;

        printf("Enter string ");
        fflush(stdout);
        if(gets(buf) == NULL)
            exit(EXIT_SUCCESS);

        i = tokenize(buf, tokens, MAXTOKEN);
        for (j = 0; j < i; j++)
            printf("%s\n", tokens[j]);
    }
}

See Also

libc,
string.h
ANSI Standard, §7.11.5.8
POSIX Standard, §8.1