Thread: strtok question

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    4

    strtok question

    i have a string

    "some string : something,something,something"

    is there another way to use strtok to get it to print

    something
    something
    something

    i tried using
    Code:
    strtok(string_buffer, ":");
    while (temp = strtok(NULL, ","))
    {
       printf("%s\n", temp);
    }
    and doesnt give the correct result.
    Thanks

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    TRy
    Code:
        char *p;
        char *string="some string :something,something,something";
        p=strchr(string,':');    
        p++;
        p=strtok(p,",");
        while(*p)
        {
             printf("%s\n",p);
             p=strtok('\0',",");
        }
        return 0;

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > char *string="some string :something,something,something";
    String constants are read-only, strtok() modifies the string.

    Use
    char string[]="some string :something,something,something";
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Yup. My bad.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    4
    thanks for that

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