#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Using STRTOK with different variables


    hi guys,

    lets say i have a variable declared as follows
    char str[] = "hello what's up?";

    and I want to use strtok, how can i store each work in a different variable using strtok?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    I recommend an array. You'll only have to use an array once after several times writing
    Code:
    work1 = strtok(args);
    work2 = strtok(other args);
    work3 = strtok(more args);
    /* oh no now I need to process a 6 work sentence */
    to realize that arrays are a better concept. Better than sliced bread. Wait! Arrays are like sliced bread. Well, a little bit.
    Last edited by b49P23TIvg; March 14th, 2013 at 09:02 PM. Reason: Tense change by inserting a missing character.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Read the documentation for strtok, which you can find by Google'ing on man page strtok; eg at http://man7.org/linux/man-pages/man3/strtok.3.html.

    Also, we're not sure what you're asking here.

    Are you asking how to extract multiple tokens out of the same string? If that's the case, then understand how strtok works and how you're supposed to use it.

    strtok works with an internal static buffer. So the first time you call it, you need to pass in the string you want it to tokenize, in which case it will load that string into that internal static buffer. Then to continue working with that same string, you pass in NULL. And to start working with a different string, you pass in that new string instead of NULL.

    The return value of strtok is a pointer to the token that was found, or NULL if none was found. So to get the next token, you call strtok again. If I understand what you want, each time you call strtok, you need to save the token that is returned to you. Now, strtok changes the original string that you pass to it and I think that it replaces the delimiter it finds with a null-terminator, but don't quote me on that. The way that we use strtok is to either immediately process the token (eg, convert a number token to an int) or to strcpy the token to a string.

    Now, if you instead want to be tokenizing more than one string at the same time, then you can't do that with strtok because of that static internal buffer. However, you may be able to do it with the reentrant version, strtok_r. I'm not familiar with that one, but you can read up on it in the man page.
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    strtok works "in-place" by replacing delimiters with nul and returning a pointer to the start of terminated substring. If you want the data in strings associated with variables you can either assign each extracted token to a pointer variable (zero-copy) or use strcpy() to move the content do an array variable.

IMN logo majestic logo threadwatch logo seochat tools logo