Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ 3.0r4 - <b>strtok</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
strtok

Usage

   #include <string.h>
   char * strtok(char * string1, const char * string2);

   ANSI

Description

   strtok parses string1 into tokens delimited by characters in string2. It
   returns a pointer to the first character in string1 that is not one of
   the delimiting characters, and writes a '\0' at the position of the next
   delimiter following this. As an internal record is stored of the current
   position within the string a subsequent call of strtok with a NULL value
   for string1 will continue parsing from the position reached in the
   previous call. string2 may change between calls.

Example 

   /*
           Program that parses a string into individual words.
           These are then stored in an array and printed
   */
   #include <string.h>
   #include <stdio.h>
   #include <stdlib.h>
   static char str[] = " \n\t\rfirst\n\tsecond\v\t third";

   int main()
   {
       char *token;
       char ** numbers;
       int i,dimension=0;
       numbers = malloc(++dimension * sizeof(char *));

   /*
           token points at the first character in str not to be
           found in    the second argument to strtok (the delimiter
           string). str is then parsed and a null written at the
           next delimiter found.
   */
       token = strtok(str,"\n \t\r\v");
       for(i=0;token ;)
       {
           tokenlist=(char **) realloc(tokenlist, ++dimension *
               sizeof(char *));
           tokenlist[i] = malloc(strlen(token) + 1);
           strcpy(tokenlist[i],token);

           tokenlist[++i] = 0;
   /*
           strtok remembers where it is in the string. The next
           call continues the parsing of the string. The delimiter
           string can be different between calls if desired
   */
           token = strtok(NULL,"\n \t\r\v");
       }
       for(i=0;tokenlist[i];i++)
           printf("%s\n",tokenlist[i]);
       return EXIT_SUCCESS;
   }

Return Value

   Returns a pointer to the next token in string1.





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