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

    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    1

    Strtok with space as delimiter


    How does it work?
    I tried
    Code:
    var1=strtok(string,"");
    var2=strtok(NULL,"");
    var1 is ok, but var2 doesn't change...

    I tried the input "1 2" (1<space>2), and I used atoi on var1 and var2, it converted var1 to 1, but keeps var2 as 0.
    now I noticed that it took the value of 2 and sent it as a paremeter to the next part of the program...
    What going on there?! :confused:
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Originally Posted by idobe
    How does it work?
    I tried
    Code:
    var1=strtok(string,"");
    var2=strtok(NULL,"");
    var1 is ok, but var2 doesn't change...

    I tried the input "1 2" (1<space>2), and I used atoi on var1 and var2, it converted var1 to 1, but keeps var2 as 0.
    now I noticed that it took the value of 2 and sent it as a paremeter to the next part of the program...
    What going on there?! :confused:
    Why are you using strok on NULL?

    What do you mean about the next part of the program?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    1
    From the input "1 2" (as a string), I try to create 2 integers of 1 and 2.
    I used this tutorial: http://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm
    I used NULL because that how it is explained there, and it worked for me until now (still works with delimiter that isn't space).
    the next part of the program gets an int, and in this case it gets 2 (the value that was supposed to be assigned to var2...)
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    The first argument to strtok is NOT const char* . This example uses an intermediate buffer. Also, are you quite sure your token strings include a space character?
    Code:
    #include<string.h>
    #include<stdio.h>
    int main() {
      char*var1,*var2,data[99];
      strcpy(data,"1 2");
      var1=strtok(data," ");
      printf("strtok changed the data: %s\n",data);
      var2=strtok(NULL," ");
      puts(var1);
      puts(var2);
      printf("strtok did not repair the data: %s\n",data);
      return 0;
    }
    Last edited by b49P23TIvg; January 9th, 2014 at 01:58 PM. Reason: new code version demonstrates strtok changes the string to be tokenized
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    1
    Well, with strcpy it works, but what do I do if I want to get a string from the user?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    read some books,
    write practice programs of increasing difficulty.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Here is the code snippet you gave us:
    Code:
    var1=strtok(string,"");
    var2=strtok(NULL,"");
    strtok returns the substring starting at the beginning of the string (if you give it a string) or where it last left off (if you pass it NULL, meaning it's to continue using the string you had passed it before) until it either encounters one of the characters in the string which is the second argument or it reaches the end of the string.

    You talk about that second string containing a space, but that's not what you passed it:
    this is a string containing a space -- " "
    this is a string containing nothing, AKA an "empty string" -- ""
    Do you see the difference between a space and an empty string?
    Can you identify which one you used?

    An additional test you should have applied would have been to print out the strings being pointed to by var1 and var2. It looks to me that var1's string is actually "1 2" and var2's is either "" or NULL. OTOH, if you had passed strtok an actual space string (ie, " ") instead of an empty string (ie, ""), then var1's string should have been "1" and var2's "2". But you didn't, so they aren't.

    atoi is a problematic function. It will start converting with the first character and continue to convert until it hits a non-digit character at which point it stops. And if the very first character is not a digit, then it will return a zero. Which is also what atoi will return if you feed it a string that actually is zero (ie, "0"). So when atoi returns a zero, you have no way to know whether it actually successfully converted a "0" or it failed to convert the string because the string wasn't a number; that's the big problem with using atoi.

    So, atoi("1 2") will convert the '1' and then quit when it hits the space, so it converts "1 2" to 1. What does atoi("") return? It fails to convert because that is not a valid numeric string, so it returns zero.

    That's what I read as happening. Tell us what you get when you printf var1 and var2.

IMN logo majestic logo threadwatch logo seochat tools logo