Thread: question about strtok?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    question about strtok?

    Hello..
    I have question for strtok..
    cause i looked at the man page..
    but i dont really get it..
    anyone teach me how to use strtok??

    I have string called
    Code:
    everything declared up there..
    score = "3,4";
    
    strtok(blahblah..);
    how can I break it use strtok??
    acutally what I wanna do is make

    I want to make like this
    score 1 contains 3
    score 2 contains 4

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    It's good to take a look in your compiler's help files first. Can't help you too much but I found a short example code which aims at explaining how it works and how to write another code with strtok.

    Code:
    /* STRTOK.C: In this program, a loop uses strtok
     * to print all the tokens (separated by commas
     * or blanks) in the string named "string".
     */
    
    #include <string.h>
    #include <stdio.h>
    
    char string[] = "A string\tof ,,tokens\nand some  more tokens";
    char seps[]   = " ,\t\n";
    char *token;
    
    void main( void )
    {
       printf( "%s\n\nTokens:\n", string );
       /* Establish string and get the first token: */
       token = strtok( string, seps );
       while( token != NULL )
       {
          /* While there are tokens in "string" */
          printf( " %s\n", token );
          /* Get next token: */
          token = strtok( NULL, seps );
       }
    }
    hope it helps

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    Sorry, it's just a microsoft example

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    52
    Here is a little bit of code, but not much..

    Ok, take this sentence:
    Pearl Jam - Alive.mp3
    If you want the artist and song name, you would use "-" as the token here, not a space.

    Code:
    int main () {
        char blah[] = "Pearl Jam - Alive.mp3";
        char *artist, *song;
    
        /* Ok, we want everything from the start of "blah" UNTILL the "-" */
        artist = strtok(blah, "-");
    
        /* Now we want everything from the current space (NULL) to the end of the line ("") */
        song = strtok(NULL, "");
    
        printf("Artist: %s - Song: %s", artist, song);
        return(0);
    }
    So basically, strtok(StartPosition, "EndPosition"); means you return everything from StartPosition to "EndPosition".

    Hope this helps a little

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. a question about strtok.
    By Masterx in forum C++ Programming
    Replies: 24
    Last Post: 11-18-2008, 11:09 PM
  3. strtok question
    By neandrake in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2003, 03:51 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM