strtok

This is a discussion on strtok within the C Programming forums, part of the General Programming Boards category; What is the correct code using strtok to allow text to be entered separated by either a space, or / ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    Question strtok

    What is the correct code using strtok to allow text to be entered separated by either a space, or /

    ?
    I tried this, but its not working correctly:
    Code:
    p=strtok(input, "'/' ' '");

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    here is an example with strtok:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    
        char *p;
    
        p = strtok("Hello World"," ");
    
        printf(p);
    
        do {
            p = strtok('\0', ", ");
            if (p)
    
              printf("%s", p);
          } while (p);
    
       return 0;
    }
    Here space and comma are delimiters

    Mr. C

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    51
    When i strchr is used to just allow for /, it works fine, but when i change it to strtok to allow for both / or a space, it outputs the wrong output. Any reason why this is happening?

    Code:
    d=atoi(input);
             p=strtok(input, "/ ");
             m=atoi(++p); 
             p=strtok(p,  "/ " );
             y=atoi(++p); 
             initcalender(d,m,y);

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    one thing you should be aware of is to always use strtok on a copy of your string and not the string itself. strtok has a habit of modifying the string u pass it.
    besides strtok should not really be used for delimiting. its use is for separating a string into its constituent tokens.To find a choice of delimiters check the functions in string.h especially strpbrk()
    Last edited by Stoned_Coder; 09-13-2002 at 10:24 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    C-Dumbie
    Guest
    Or you can use strchar() for filtering diliminers such as ; ?..
    C-Dumbie

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    Code:
    d=atoi(input);
             p=strtok(input, "/ ");
             m=atoi(++p); 
             p=strtok(p,  "/ " );
             y=atoi(++p); 
             initcalender(d,m,y);
    should read:

    Code:
    d=atoi(input);
             p=strtok(input, "/ ");
             m=atoi(++p); 
             p=strtok(NULL,  "/ " );
             y=atoi(++p); 
             initcalender(d,m,y);
    Basically, you only want to pass strtok() a string the first time you call it. strtok() will actually keep track of which string it is currently working on, and when it is passed NULL as the first argument on subsequent calls, it will continue to try and tokenize the string it began working on with the first call.

    And if maintaining the original string is important, you will definitely be wanting to copy it first, as strtok() will chop up your string into the pieces you ask for.

    A second alternative to your first question about token syntax is to actually create a string of seperators. This comes in handy if you will be using the same seperators a lot; it cuts down on typos:

    Code:
    char *seperators = " /";
    Hope this helps,

    starX
    www.axisoftime.com

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, 04:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21