strtok with "" as delimiter?

This is a discussion on strtok with "" as delimiter? within the C Programming forums, part of the General Programming Boards category; Hi, can someone please clarify if I can call strtok like below: ptr = strtok (originalStr, ""); and it should ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    27

    strtok with "" as delimiter?

    Hi, can someone please clarify if I can call strtok like below:

    ptr = strtok (originalStr, "");

    and it should work the same as this?

    prt = strtok (originalStr, " ");

    I want to tokenize strings separated by a space. And from what I understand of the specifications in the standard, the two versions shouldn't work the same right? Actually, the first version shouldn't work at all?

    I'm using the second version now. And the 1st version doesn't seem to work on the compiler I'm using. But someone's telling me that the first one should work the same. Is this some weird exception in the specification or it only works with some compilers? Thanks for any help.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Looking for nothing and looking for a space character to me sound like two different things. Why should they be the same?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Exactly what part of the specification are you referring to that says "" is the same as " "? If people would just stop listening to all these someone-I-knows, they might not be so confused. The someone-I-knows seem to never know anything.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    27
    Actually, let me clarify. I don't think they're supposed to work the same. In fact, I think calling strtok with the first version

    ptr = strtok (originalStr, "");

    would set ptr to point to originalStr, exactly the same string.

    And calling with the second version would return the first token to ptr, as expected.

    But my boss is telling me it works the same. So I just want to be absolutely sure before I say anything different. That's why I'm asking here since you guys are expert. Thanks.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,035
    The Key points here are:
    Whitespace characters are not ignored when part of literal strings.
    Nor are they ignored in macros, though that is only meaningfull for the enter key.

    Otherwise, whitespace characters are ignored in C code.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Don't forget character constants (e.g. ' ')
    If you understand what you're doing, you're not learning anything.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by fanoliv
    But my boss is telling me it works the same. So I just want to be absolutely sure before I say anything different. That's why I'm asking here since you guys are expert. Thanks.
    Ask the boss to try an extremely simple example and explain the results.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char text[] = "one two three", *ptr;
       puts(strtok (text, ""));
       puts(strtok (text, " "));
       return 0;
    }
    
    /* my output
    one two three
    one
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > But my boss is telling me it works the same.
    Looks like he got promoted for incompetence rather than being fired for incompetence.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Does your boss know how to code at all? No: get a new boss who isn't a smartass.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    27
    lol...my boss is a pretty nice guy actually. And I'm sure he knows a lot more than I do about programming. Thanks for the clarification though.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    57
    In the second arguement of strtok you can place many characters and the function will tokenize on each character

    strtok(string_to_tokenize, " ,e&");

    Will tokenize on occurence of whitespace, comma, 'e' and '&' in string, replacing each occurence with null.

    of course for

    strtok(string_to_tokenize, "");

    will tokenize on nothing, and will not replace anything with null, the string will be unchanged.

    Note also, companies are researching new hires and employees on the web all the time. Nothing said on the internet is safe!

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't remember putting my name down as 'quzah' on my last job application.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Maybe it would help! Especially if you showed up for your interview dressed up as combra commander.
    If you understand what you're doing, you're not learning anything.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I would be immediately promoted, just so I could walk around screaming:

    "Morons! I have morons on my payroll!"

    And if I wasn't promoted, I could always just kill everyone higher up the ladder...


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by darsunt
    Will tokenize on occurence of whitespace, comma, 'e' and '&' in string, replacing each occurence with null.

    Last time I checked, strtok did not change any part of the string passed to it.

    [EDIT]

    My mistake, you are entirely correct; I had just never tried to access an array after I strtok'ed it.
    Last edited by bivhitscar; 06-17-2006 at 03:03 AM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Page 1 of 2 12 LastLast
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. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  3. scanf - strtok and space as delimiter?
    By Bill Cosby in forum C Programming
    Replies: 6
    Last Post: 09-20-2004, 06:45 PM
  4. strtok tokenizing on spaces as well as my delimiter
    By snowblind37 in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2004, 12:39 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