Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C Programming Language

strtok as string replace function problem ?


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;

In article <1181075500.613448.189@q69g2000hsb.googlegroups.com>,
Lothar Behrens  <lothar.behr@lollisoft.de> 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

On 5 Jun., 22:54, rober@ibd.nrc-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 wrote:
> On 5 Jun., 22:54, rober@ibd.nrc-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

> > 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 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

Lothar Behrens wrote, On 05/06/07 22:56:

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
On 6 Jun., 00:33, Flash Gordon <s@flash-gordon.me.uk> 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 wrote:
> On 6 Jun., 00:33, Flash Gordon <s@flash-gordon.me.uk> 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

None of these things are the normal tools available.  C has long
since been standardized, including the library.  Read the standard
and use those tools.  Otherwise you are off-topic on c.l.c.

--
 <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
 <http://www.securityfocus.com/columnists/423>
 <http://www.aaxnet.com/editor/edit043.html>
 <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
                        cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc