Thread: strtok intricacies

  1. #1
    Unregistered
    Guest

    Question strtok intricacies

    Does anybody know how strtok works?

    What mean to say is, it is in the string.h library but all that is written there is

    char * strtok(char *str, const char *sep)

    Does anybody actually know the code for strtok, i.e. how it actually breaks strings into seperate parts?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This should be in the C board
    Code:
    #include <string.h>
    
    char *strtok ( char *a, const char *b )
    {
      static char *save = "";
      char *start, *end;
      start = a ? a : save;
      start += strspn ( start, b );
      if ( *start == '\0' ) {
        save = "";
        return NULL;
      }
      end = start + strcspn ( start, b );
      if ( *end != '\0' )
        *end++ = '\0';
      save = end;
      return start;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  3. converting string to integer, for further use
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 07-01-2005, 03:12 AM
  4. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM