Thread: strstr and strtok questions.....

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

    strstr and strtok questions.....

    Hello all,

    Regarding strstr...

    If I declare l_start has a pointer to char:
    char *l_start

    Can I use strstr on the same pointer? i.e.

    l_start = strstr(v_string, "*");
    l_start++;
    l_start = strstr(l_start, "*");
    l_start++;
    strcpy(v_substring, l_start);

    Regarding strtok...

    Can the token being used in strtok be more than one character in length i.e
    &
    or
    ABC&&&

    Thanks for your help.

    Manny

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can I use strstr on the same pointer?
    Yes.

    >an the token being used in strtok be more than one character in length
    No.
    My best code is written with the delete key.

  3. #3
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Actually, the token in strtok can be more than one character. However, this won't search for a token of "ABC&&&" - it will search for any of "A" "B" "C" "&" "&" "&" ( thus making the extra &'s redundant ).
    Last edited by bivhitscar; 06-06-2006 at 10:39 PM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're misunderstanding the statement. A token is a single character. It doesn't matter if you repeat the character or not, you'll still only have one character per 'token'. That is to say, you can't have "AB" be what it searches for; where it looks for an A immediately followed by a B.

    IE: It does not "pattern match".

    Thus, each token is a single character. (It isn't strstr.)


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

  5. #5
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Were you disagreeing with me, or making a different point?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, it seems we're at a misunderstanding as to what token means. According to what it's being described as in this thread, the following would be my reply:

    You cannot make a token be more than one character. A token is a single character that strtok searches for for its "stop point". Thus, you cannot make the token be more than one character. Just as I mentioned. Example:
    Code:
    char words[] "blah blah bla blaba bla";
    char token[] = "ab";
    char *ptr;
    
    ptr = strstr( words, token ); /* Will find the multi-character token "ab". */
    ptr = strtok( words, token ); /* Won't. Will stop at the first character in the string. */
    However, if we use token as described by the strtok man page, then the reply would have to be different. They describe it as:

    A `token' is a nonempty string of characters not occurring
    in the string delim, followed by \0 or by a character
    occurring in delim.

    If we use that definition of token, then it will almost always be multiple characters. In fact, it will be multiple characters if there is ever any occurance of a non-delims character at all. Because it will have a null stuck on the end, making it multiple characters.

    So... it depends what the OP meant by token I guess.


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

  7. #7
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Yeh, defintely a misunderstanding.

    I was using the word token to refer to the particular argument that is passed to strtok. I guess my explanation was a little vague.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Popular pages Recent additions subscribe to a feed