Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C - <b>strtok() finds next token in string</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
strtok()                 Finds Next Token in String

 #include   <string.h>                   Required for declarations only

 char       *strtok(string1,string2);
 char       *string1;                    String containing tokens
 char       *string2;                    Delimiter characters

    strtok() considers 'string1' to be a sequence of zero or more tokens,
    each separated by one or more delimiters.  'string2' is the set of
    characters serving as delimiters of the tokens in 'string1'.

    The tokens in 'string1' are extracted by a series of calls to
    strtok().  In the first call to strtok() with a given 'string1',
    strtok() will search for the first token, skipping leading
    delimiters, and return a pointer to the first token.  To read the
    next token, call strtok() with a NULL 'string1', which will cause
    strtok() to continue searching for the next token in the previous
    string.  You can change 'string2' between calls.

    Returns:    A pointer to the first token in 'string1' is returned on
                the first call to strtok().  In subsequent calls with the
                same 'string1' (indicated by a NULL argument for
                'string1'), strtok() returns a pointer to the next token
                in the string.  A NULL pointer is returned when there are
                no more tokens.

      Notes:    strtok() modifies 'string1'; each time it is called, it
                inserts a NULL value after the token found.

  -------------------------------- Example ---------------------------------

    The following statements retrieve and print tokens from 'str1'.

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

         char *str1 = "apples, oranges, bananas, pears.";
         char *delim = " ,.";
         char *tokens;

         main()
         {

              while((tokens = strtok(str1,delim)) != NULL)
                      printf("%s\n",tokens);
         }

See Also: strcspn() strspn()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson