strtok

Discussion in 'C Programming' started by gyan, Nov 25, 2005.

  1. gyan

    gyan Guest

    Hi

    How strtok track through string?
    char *strtok(char *s1, const char *s2);
    As i know
    The first call (with pointer s1 specified) returns a pointer to the
    first character of the first token, and will have written a null character
    into s1 immediately following the returned token. The function keeps track
    of its position in the string between separate calls, so that
    subsequent calls (which must be made with the first argument being a
    null pointer) will work through the string s1 immediately following
    that token.

    Now suppose i have
    char *ptr = "Hello:world";
    char *str = "Drop:me";
    printf("\n%s",strtok(ptr,":"));
    printf("\n%s",strtok(str,":"));

    above 2 lines will give output as
    Hello
    Drop
    Now if i want to call strtok to get next part of string "ptr", what i
    should do?since calling
    printf("\n%s",strtok(NULL,":"))
    will give 2nd part of str and not of ptr.
    gyan, Nov 25, 2005
    #1
    1. Advertising

  2. gyan said:

    > Hi
    >
    > How strtok track through string?
    > char *strtok(char *s1, const char *s2);
    > As i know
    > The first call (with pointer s1 specified) returns a pointer to the
    > first character of the first token, and will have written a null character
    > into s1 immediately following the returned token. The function keeps
    > track
    > of its position in the string between separate calls, so that
    > subsequent calls (which must be made with the first argument being a
    > null pointer) will work through the string s1 immediately following
    > that token.
    >
    > Now suppose i have
    > char *ptr = "Hello:world";
    > char *str = "Drop:me";
    > printf("\n%s",strtok(ptr,":"));


    Stop right there. strtok modifies the string you pass to it. Here, you are
    passing it the start address of a string literal. Use an array instead:

    char haystack[] = "Hello:world:eek:f:fun:and:games:with:C";
    char *token;
    for(token = strtok(haystack, ":"); token != NULL; token = strtok(NULL, ":"))
    {
    printf("%s\n", token);
    }


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Nov 25, 2005
    #2
    1. Advertising

  3. gyan

    Eric Sosman Guest

    gyan wrote:

    > Hi
    >
    > How strtok track through string?
    > char *strtok(char *s1, const char *s2);
    > As i know
    > The first call (with pointer s1 specified) returns a pointer to the
    > first character of the first token, and will have written a null character
    > into s1 immediately following the returned token. The function keeps track
    > of its position in the string between separate calls, so that
    > subsequent calls (which must be made with the first argument being a
    > null pointer) will work through the string s1 immediately following
    > that token.
    >
    > Now suppose i have
    > char *ptr = "Hello:world";
    > char *str = "Drop:me";
    > printf("\n%s",strtok(ptr,":"));
    > printf("\n%s",strtok(str,":"));
    >
    > above 2 lines will give output as
    > Hello
    > Drop
    > Now if i want to call strtok to get next part of string "ptr", what i
    > should do?since calling
    > printf("\n%s",strtok(NULL,":"))
    > will give 2nd part of str and not of ptr.


    See Richard Heathfield's response for a warning about
    a serious error in this code.

    As to "How do I use strtok() on one string, interrupt
    it to use strtok() on another, and then resume processing
    the first," the answer is that you don't. strtok() uses
    its own internal storage to keep track of how far it's
    progressed through your string, and it can keep track of
    only one position. That is a weakness in the design of
    the function; there are others.

    Some C implementations may provide the non-Standard
    strtok_r() function that avoids this particular drawback
    of strtok() -- while perpetuating the others. Perhaps it
    will suit your needs. If it isn't available or if its
    other weaknesses make trouble for you, C has a fairly rich
    set of low-level string-bashing functions like strchr() and
    strcspn() from which you can probably build whatever more
    advanced capabilities you need.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 25, 2005
    #3
  4. On Fri, 25 Nov 2005 01:29:29 -0500, "gyan" <> wrote:

    >Hi
    >
    >How strtok track through string?
    >char *strtok(char *s1, const char *s2);
    >As i know
    >The first call (with pointer s1 specified) returns a pointer to the
    >first character of the first token, and will have written a null character
    >into s1 immediately following the returned token. The function keeps track
    >of its position in the string between separate calls, so that
    >subsequent calls (which must be made with the first argument being a
    >null pointer) will work through the string s1 immediately following
    >that token.
    >
    >Now suppose i have
    >char *ptr = "Hello:world";
    >char *str = "Drop:me";
    >printf("\n%s",strtok(ptr,":"));
    >printf("\n%s",strtok(str,":"));
    >
    >above 2 lines will give output as
    >Hello
    >Drop
    >Now if i want to call strtok to get next part of string "ptr", what i
    >should do?since calling
    >printf("\n%s",strtok(NULL,":"))
    >will give 2nd part of str and not of ptr.


    After you fix the problem with passing the address of a string literal
    as the first argument, you could do something like
    ptr = strtok(ptr+strlen(ptr)+1,":");
    but you will have to do your own checking for when you reach the end
    of your original string.



    <<Remove the del for email>>
    Barry Schwarz, Nov 25, 2005
    #4
  5. gyan

    Guest

    The strtok modifies the original string so you must first create a
    duplicate of that string or copy it to another string, so the changes
    apply to the another string and not the original; do this if u want to
    keep your original string as it was.

    Another point to say is that the strtok drops you the charachters after
    and before that a NULL charachter was found, example: "Hello;my;friend"
    in this case if your NULL char is ; the strings dropped by the strtok
    function should be:
    1. Hello
    2. my
    3. friend

    remember save the original string in another string and use the strtok
    function to get the tokens from the secondary string so you wont lose
    any data from the original String
    , Dec 5, 2005
    #5
  6. On 5 Dec 2005 15:31:50 -0800, in comp.lang.c ,
    wrote:
    (contextless remarks)
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Dec 5, 2005
    #6
  7. gyan

    pete Guest

    wrote:

    > NULL charachter


    You misspelled "null" and "character".

    --
    pete
    pete, Dec 5, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam Balgach
    Replies:
    2
    Views:
    559
    news-east
    Nov 28, 2004
  2. Alex Vinokur

    strtok() and std::string

    Alex Vinokur, Apr 14, 2005, in forum: C++
    Replies:
    6
    Views:
    4,897
    Pete Becker
    Apr 14, 2005
  3. strtok problem

    , Aug 28, 2003, in forum: C Programming
    Replies:
    4
    Views:
    499
  4. Robert

    strtok trouble

    Robert, Sep 5, 2003, in forum: C Programming
    Replies:
    17
    Views:
    1,212
    Jalapeno
    Sep 6, 2003
  5. Fatih Gey

    segfault on strtok

    Fatih Gey, Oct 23, 2003, in forum: C Programming
    Replies:
    40
    Views:
    1,439
    nobody
    Nov 1, 2003
Loading...

Share This Page