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 6.0 - <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
 const 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 that serve 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() skips leading delimiters, then searches for and returns a
    pointer to the first token.  Calling strtok() with a NULL 'string1',
    then causes strtok() to search 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' because it inserts a NULL
                value after the token found each time it is called.

   -------------------------------- 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()
           {
               tokens = strtok(str1,delim);
               printf("%s\n",tokens);
               while((tokens = strtok(NULL,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