strtok as string replace function problem ?

Discussion in 'C Programming' started by Lothar Behrens, Jun 5, 2007.

  1. Hi,

    I have selected strtok to be used in my string replacement function.
    But I lost the last token, if there is one.

    This string would be replaced

    select "name", "vorname", "userid", "passwort" from "users" order by
    "users"

    by this one:

    select "name", "vorname", "userid",
    "passwort" from "users" order by "users

    Thus the last quot is lost.

    Is there something missing in the code ?

    Thanks, Lothar

    lb_I_String& LB_STDCALL lbString::replace(const char* toReplace, const
    char* with) {
    // Don't worry about my object variables :)
    UAP_REQUEST(getModuleInstance(), lb_I_String, rep)

    // the string
    char* token = strtok(stringdata, toReplace);

    if ((token != NULL) && (token != stringdata)) {
    *rep += with;
    }

    while(token != NULL)
    {
    *rep += token;
    token = strtok(NULL, toReplace);
    if (token != NULL) *rep += with;
    }

    setData(rep->charrep());

    return *this;
    }
     
    Lothar Behrens, Jun 5, 2007
    #1
    1. Advertising

  2. In article <>,
    Lothar Behrens <> wrote:

    >I have selected strtok to be used in my string replacement function.
    >But I lost the last token, if there is one.


    >This string would be replaced


    >select "name", "vorname", "userid", "passwort" from "users" order by
    >"users"


    >by this one:


    >select &quot;name&quot;, &quot;vorname&quot;, &quot;userid&quot;,
    >&quot;passwort&quot; from &quot;users&quot; order by &quot;users


    >Thus the last quot is lost.


    >Is there something missing in the code ?



    >lb_I_String& LB_STDCALL lbString::replace(const char* toReplace, const
    >char* with) {


    Are you sure this isn't C++? The :: is suspicious, and the & cannot
    occur in that position in C.

    > // Don't worry about my object variables :)



    > // the string
    > char* token = strtok(stringdata, toReplace);


    The first call in the sequence searches the string pointed to
    by s1 for first character that is not contained in the current
    separator string pointed to by s2. If no such character is
    found, then there are no tokens in the string pointed to by s1
    and the strtok function returns a null pointer."

    What this implies is that if the string ends in the delimiter,
    then strtok() will *not* return a pointer to the empty line.
    srtrok() effectively cannot distinguish between the case of
    the string ending in \0 and the string ending in any number
    of the given delimiters: you have to do that yourself by
    keeping track of where the last token ends and comparing that to
    the known position of the end of the string.
    --
    I was very young in those days, but I was also rather dim.
    -- Christopher Priest
     
    Walter Roberson, Jun 5, 2007
    #2
    1. Advertising

  3. On 5 Jun., 22:54, -cnrc.gc.ca (Walter Roberson) wrote:
    >
    > Are you sure this isn't C++? The :: is suspicious, and the & cannot
    > occur in that position in C.
    >


    Yes, sorry. But I put the complete function to indicate C++ code.
    Because there
    are also + operators used :)

    >
    > What this implies is that if the string ends in the delimiter,
    > thenstrtok() will *not* return a pointer to the empty line.
    > srtrok() effectively cannot distinguish between the case of
    > the string ending in \0 and the string ending in any number
    > of the given delimiters: you have to do that yourself by
    > keeping track of where the last token ends and comparing that to
    > the known position of the end of the string.


    Using a flag and determining a trailing token would then help...

    Thanks

    Lothar
     
    Lothar Behrens, Jun 5, 2007
    #3
  4. Lothar Behrens

    Default User Guest

    Lothar Behrens wrote:

    > On 5 Jun., 22:54, -cnrc.gc.ca (Walter Roberson) wrote:
    > >
    > > Are you sure this isn't C++? The :: is suspicious, and the & cannot
    > > occur in that position in C.
    > >

    >
    > Yes, sorry. But I put the complete function to indicate C++ code.


    Which means you are in the wrong newsgroup. You want comp.lang.c++.
    Actually, you also probably want to do the problem quite differently.




    Brian
     
    Default User, Jun 5, 2007
    #4
  5. > > Yes, sorry. But I put the complete function to indicate C++ code.
    >
    > Which means you are in the wrong newsgroup. You want comp.lang.c++.
    > Actually, you also probably want to do the problem quite differently.
    >
    > Brian


    Hmmm,

    didn't you read the subject ?

    I have asked for the strtok function. This is a C function and
    therefore NOT wrong here.
    If I do ask this question in comp.lang.c++, they probably tell me I am
    wrong there.

    The posting rules here are too strong :-(

    Lothar
     
    Lothar Behrens, Jun 5, 2007
    #5
  6. Lothar Behrens

    Default User Guest

    Lothar Behrens wrote:

    > > > Yes, sorry. But I put the complete function to indicate C++ code.

    > >
    > > Which means you are in the wrong newsgroup. You want comp.lang.c++.
    > > Actually, you also probably want to do the problem quite
    > > differently.
    > >
    > > Brian

    >
    > Hmmm,
    >
    > didn't you read the subject ?


    Of course.

    > I have asked for the strtok function. This is a C function and
    > therefore NOT wrong here.


    It's also a C++ function, and that's the language you're working in.

    > If I do ask this question in comp.lang.c++, they probably tell me I am
    > wrong there.


    No. Well some might, but I'll be there to correct them. Besides, as I
    said, you don't want to do it that way. Besides, I see that you DID
    post over there, so you can't be too worried.

    I've given you a better solution there. If you whinge that it didn't
    answer the question, I'll severely killfile you about the head and
    shoulders.

    > The posting rules here are too strong :-(


    Not really. Ask questions about C. Lurk and/or read over a few weeks
    worth of posts.





    Brian
     
    Default User, Jun 5, 2007
    #6
  7. Lothar Behrens

    Flash Gordon Guest

    Lothar Behrens wrote, On 05/06/07 22:56:
    >>> Yes, sorry. But I put the complete function to indicate C++ code.

    >> Which means you are in the wrong newsgroup. You want comp.lang.c++.
    >> Actually, you also probably want to do the problem quite differently.
    >>
    >> Brian

    >
    > Hmmm,
    >
    > didn't you read the subject ?
    >
    > I have asked for the strtok function. This is a C function and
    > therefore NOT wrong here.
    > If I do ask this question in comp.lang.c++, they probably tell me I am
    > wrong there.
    >
    > The posting rules here are too strong :-(


    Generally deciding to use a specific function to solve a problem and
    then trying to solve it is the wrong approach. Work out the best way to
    solve the function with all of the tools available, instead of just one.
    If you do have a specific reason for wanting to use strtok them tell us.
    --
    Flash Gordon
     
    Flash Gordon, Jun 5, 2007
    #7
  8. On 6 Jun., 00:33, Flash Gordon <> wrote:

    > If you do have a specific reason for wanting to use strtok them tell us.


    This is an excerpt from http://www.openwatcom.org/index.php/Open_Watcom_STL#.3Cstring.3E

    OWSTL is only available in Open Watcom v1.4 or later releases.
    Although a few STL features do exist in Open Watcom v1.3, STL support
    in v1.3 is very minimal. If you wish to experiment with OWSTL you will
    need to install or upgrade to v1.4 or later. Naturally we recommend
    that you use the latest version of Open Watcom possible. Note also
    that copying the OWSTL headers into an installation of a pre-v1.4
    release is not likely to work. The current implementation of OWSTL
    depends on a number of namespace/template bug fixes that occured
    during the development of Open Watcom v1.4.

    I still use OW 1.3, because I had problems to move to 1.4. I'll try
    the latest if I have time.

    Regards, Lothar
     
    Lothar Behrens, Jun 6, 2007
    #8
    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. default

    home grown strtok() function for review

    default, Sep 19, 2006, in forum: C Programming
    Replies:
    4
    Views:
    358
    default
    Sep 21, 2006
  2. Lothar Behrens
    Replies:
    11
    Views:
    4,226
  3. Alun
    Replies:
    3
    Views:
    4,523
    Masudur
    Feb 18, 2008
  4. Prasad S
    Replies:
    2
    Views:
    236
    Dr John Stockton
    Aug 27, 2004
  5. V S Rawat
    Replies:
    5
    Views:
    309
    Richard Cornford
    Jul 3, 2007
Loading...

Share This Page