ssttrrttookk() -- String Function (libc)

Break a string into tokens
#iinncclluuddee <ssttrriinngg.hh>
cchhaarr *ssttrrttookk(_s_t_r_i_n_g_1, _s_t_r_i_n_g_2)
cchhaarr *_s_t_r_i_n_g_1, *_s_t_r_i_n_g_2;

ssttrrttookk() helps to divide a string  into a set of tokens.  _s_t_r_i_n_g_1 points to
the string to be divided, and _s_t_r_i_n_g_2 points to the character or characters
that delimit the tokens.

ssttrrttookk() divides a string into tokens by being called repeatedly.

On the  first call to  ssttrrttookk(), _s_t_r_i_n_g_1 should  point to the  string being
divided.  ssttrrttookk()  searches for  a character  that is _n_o_t  included within
_s_t_r_i_n_g_2. If it finds one, then  ssttrrttookk() regards it as the beginning of the
first  token within  the string.   If  one cannot  be found,  then ssttrrttookk()
returns NULL  to signal that the  string could not be  divided into tokens.
When the beginning  of the first token is found,  ssttrrttookk() then looks for a
character  that _i_s  included within  _s_t_r_i_n_g_2. When  one is  found, ssttrrttookk()
replaces  it with  a null  character to  mark the end  of the  first token,
stores a  pointer to the remainder  of _s_t_r_i_n_g_1 within a  static buffer, and
returns the address of the beginning of the first token.

On subsequent calls to ssttrrttookk(),  set _s_t_r_i_n_g_1 to NULL.  ssttrrttookk() then looks
for subsequent tokens, using the address that it saved from the first call.
With each call  to ssttrrttookk(), _s_t_r_i_n_g_2 may point to  a different delimiter or
set of delimiters.

_E_x_a_m_p_l_e
The following example breaks ccoommmmaanndd_ssttrriinngg into individual tokens and puts
pointers  to the  tokens into  the array ttookkeennlliisstt[].  It then  returns the
number of  tokens created.  No  more than mmaaxxttookkeenn tokens  will be created.
ccoommmmaanndd_ssttrriinngg is modified to  place '\0' over token separators.  The token
list  points into  ccoommmmaanndd_ssttrriinngg. 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]);
    }
}

_S_e_e _A_l_s_o
lliibbcc, ssttrriinngg.hh
ANSI Standard, section 7.11.5.8
POSIX Standard, section 8.1
