Thread: Strtok

  1. #1
    Unregistered
    Guest

    Strtok

    can someone please show me an example of strtok i am reading in a string from a file and i want to lets say take all the words that end in : and put them into a linked list for this application words ending in : are going to be deffintions so they will go into the st def; field of a node in a linked list that contains 3 fields per node
    here is my struct

    struct node{
    int ln;
    st id;
    st def;
    struct node* next;
    }

    Im just not quite sure how strtok would be used in this application
    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why use strtok? Do you have to for this assignment? I've already told you how to fix this without using strtok. Anyway:

    char *tokiestring = "abc def ghi jkl mno pqr";
    char *c;

    c = strtok( tokiestring, " " );

    c will end up holding "abc".

    Again, I wouldn't use strtok in this application. There is no need for it, unless you're just using it to chop the first word off of a long string.

    And if you're going to do that, there are better functions anyway.

    Quzah.

  3. #3
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Cool Breaking string into tokens

    If you want to break a string of words that are all separated by ':' you could definitely use strtok(). It is not the safest function to use, and it does change the original string that you pass to it, but if you are pretty sure what kind of data you are parsing using strtok() go ahead.

    Here is what you would do:

    #define MY_DELIMITER ":"

    char MyString[] = { "abc:def:ghi:jkl:mnoqr" };

    char tokens[6][4];
    char *temp;

    temp_str = (char*)malloc(4);

    temp_str = strtok( MyString, MY_DELIMITER );
    strcpy(tokens[0], temp_str);
    printf("Token #1: %s\n", tokens[0]);

    for(int i=1; i < 6; i++) {
    temp_str = strtok( NULL, MY_DELIMITER );
    strcpy(tokens[i], temp_str);
    printf("Token #%d: %s\n", i+1, tokens[i]);
    }

    This will break the string into tokens defined by the delimiter and output all the tokens on the screen.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  4. #4
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Talking Oooops

    Please ignore this line:

    temp_str = (char*)malloc(4);

    I put it there by mistake. Sorry
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Neat! What's the ansii characterfor the smiley face hehe...

    putc( );

    Quzah.

  6. #6
    Unregistered
    Guest

    Talking Oooops

    Please ignore this line:

    temp_str = (char*)malloc(4);

    I put it there by mistake. Sorry

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. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 10:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM