Connecting Tech Pros Worldwide Forums | Help | Site Map

strtok as string replace function problem ?

Lothar Behrens
Guest
 
Posts: n/a
#1: Jun 5 '07
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;
}

Default User
Guest
 
Posts: n/a
#2: Jun 5 '07

re: strtok as string replace function problem ?


Lothar Behrens wrote:
Quote:
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 ?

Scrap the entire thing. Use std::string and its replace() member
function.

Here's an example I have laying around, it could be lot more generic.


#include <string>
#include <iostream>


int main()
{
std::string test = "1231231231\n";
std::string rep = "one";

std::string::size_type curr = 0;
while ((curr = test.find("1", curr)) != std::string::npos)
{
test.replace(curr, 1, rep);
}

std::cout << test;

return 0;
}



Brian
Lothar Behrens
Guest
 
Posts: n/a
#3: Jun 5 '07

re: strtok as string replace function problem ?


On 5 Jun., 23:54, "Default User" <defaultuse...@yahoo.comwrote:
Quote:
>
Scrap the entire thing. Use std::string and its replace() member
function.
>
In my past I had issues to avoid std::string. I do write multiplatform
code with Watcom - or
Open Watcom for now on Windows.

Therefore I tried to use strtok.

Lothar

Thomas J. Gritzan
Guest
 
Posts: n/a
#4: Jun 5 '07

re: strtok as string replace function problem ?


Lothar Behrens wrote:
Quote:
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 ?
Quote:
lb_I_String& LB_STDCALL lbString::replace(const char* toReplace, const
char* with) {
[...]
Quote:
char* token = strtok(stringdata, toReplace);
[...]

You didn't lost a token, you lost a delimiter. The second argument to
strtok is a delimiter string, and the function gives you the tokens in
between. When there is a delimiter at the end, it is lost.

Try to write the function with strchr.

Or better try the much more easily to handle std::string. :-)

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Gavin Deane
Guest
 
Posts: n/a
#5: Jun 5 '07

re: strtok as string replace function problem ?


On 5 Jun, 23:07, Lothar Behrens <lothar.behr...@lollisoft.dewrote:
Quote:
On 5 Jun., 23:54, "Default User" <defaultuse...@yahoo.comwrote:
Quote:
Scrap the entire thing. Use std::string and its replace() member
function.
>
In my past I had issues to avoid std::string. I do write multiplatform
code with Watcom - or
Open Watcom for now on Windows.
>
Therefore I tried to use strtok.
How far back in your past did you have issues with std:string? It is
no less standard than strtok and so should be perfectly suitable for
use in platform-independent code if it's the right tool for the job
(which it sounds like it might well be). I'd be surprised if any
modern C++ development tools had many issues with something as
fundamental as std::string.

If the problems you had were with a compiler developed several years
ago, you will probably find they've gone away with more up to date
compilers. If you had problems with a relatively modern compiler then,
unless it was something quite esoteric, I would think it a quality of
implementation issue of some concern.

Gavin Deane


Gavin Deane

BobR
Guest
 
Posts: n/a
#6: Jun 6 '07

re: strtok as string replace function problem ?



Lothar Behrens <lothar.behrens@lollisoft.dewrote in message ...
Quote:
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 &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 ?
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;
}
>
Please read this before your next post:
http://www.parashift.com/c++-faq-lite/how-to-post.html

Here's a (loose) example that does what you want:

#include <iostream>
#include <string>

int main(){ using std::cout // for NG post
std::string RepTest("select \"name\", \"vorname\", \"userid\",
\"passwort\" from \"users\" order by \"users\"");
// the above should be all one line.
cout<<RepTest<<std::endl;
std::string Sfind( "\"" );
std::string Sput( "&quot;" );
size_t pos;
while( ( pos = RepTest.find( Sfind ) ) != RepTest.npos ){
RepTest.erase(pos, 1);
RepTest.insert(pos, Sput);
}
cout<<RepTest<<std::endl;

return 0;
} // main()

/* - output -
select "name", "vorname", "userid", "passwort" from "users" order by "users"
select &quot;name&quot;, &quot;vorname&quot;, &quot;userid&quot;,
&quot;passwort&quot; from &quot;users&quot; order by &quot;users&quot;
*/

Should be simple to build a function from that.
(gotta leave something for you to do. <G>).

--
Bob R
POVrookie


Lothar Behrens
Guest
 
Posts: n/a
#7: Jun 6 '07

re: strtok as string replace function problem ?


On 6 Jun., 01:12, "BobR" <removeBadB...@worldnet.att.netwrote:
Quote:
Lothar Behrens <lothar.behr...@lollisoft.dewrote in message ...
>
Please read this before your next post:http://www.parashift.com/c++-faq-lite/how-to-post.html
>
Thank you all.

I'll take a look at std::string by searching in the samples of Open
Watcom for it.

BTW. I have fixed the problem if I have understood the strtok function
correctly.

Can the token be a 'multi character' string such as "\\\"" ?

(Not tokenizing '\\' and \"')

Maybe this would be a problem in my replace implementation.

Regards, Lothar

Default User
Guest
 
Posts: n/a
#8: Jun 6 '07

re: strtok as string replace function problem ?


Lothar Behrens wrote:
Quote:
BTW. I have fixed the problem if I have understood the strtok function
correctly.
>
Can the token be a 'multi character' string such as "\\\"" ?
No. It can easily be done with some small modifications to the code I
gave you earlier.




Brian


BobR
Guest
 
Posts: n/a
#9: Jun 6 '07

re: strtok as string replace function problem ?



Lothar Behrens <lothar.behrens@lollisoft.dewrote in message...
Quote:
On 6 Jun., 01:12, "BobR" wrote:
Quote:
Lothar Behrens wrote in message ...
Please read this before your next post:
>http://www.parashift.com/c++-faq-lite/how-to-post.html
>
Thank you all.
>
I'll take a look at std::string by searching in the samples of Open
Watcom for it.
Why not just try my (and/or Default User's) example. If it compiles, you're
good to go. If not, then check to see if you have the <stringheader in the
C++ include directory (and get a newer compiler if it's not there).
Quote:
>
BTW. I have fixed the problem if I have understood the strtok function
correctly.
Can the token be a 'multi character' string such as "\\\"" ?
(Not tokenizing '\\' and \"')
Do you mean the 'delimiter'? Here is an example right out of the GNU 'libC'
docs:
// note this is 'C' code.
// for 'C++', change the name 'string' to something else.

const char string[] = "words separated by spaces -- and, punctuation!";
const char delimiters[] = " .,;:!-";
char *token, *cp;
...
cp = strdupa (string); /* Make writable copy. */
token = strtok (cp, delimiters); /* token ="words" */
token = strtok (NULL, delimiters); /* token ="separated" */
token = strtok (NULL, delimiters); /* token ="by" */
token = strtok (NULL, delimiters); /* token ="spaces" */
token = strtok (NULL, delimiters); /* token ="and" */
token = strtok (NULL, delimiters); /* token ="punctuation" */
token = strtok (NULL, delimiters); /* token =NULL */

I've never used 'strtok'. I "git 'er done" with std::string and
std::stringstream.
So, if the above does not answer your question, you'll have to repost and
wait for an expert with his/her roots in 'C' (or 'strtok' experience).
If you were asking about the escape char (\), I don't know how 'strtok'
handles it. Experiment.

If you are going to code in 'C++', you really should learn std::string at a
minimum.

--
Bob R
POVrookie


Thomas J. Gritzan
Guest
 
Posts: n/a
#10: Jun 6 '07

re: strtok as string replace function problem ?


BobR wrote:
Quote:
Lothar Behrens <lothar.behrens@lollisoft.dewrote in message...
Quote:
>BTW. I have fixed the problem if I have understood the strtok function
>correctly.
>Can the token be a 'multi character' string such as "\\\"" ?
>(Not tokenizing '\\' and \"')
>
Do you mean the 'delimiter'? Here is an example right out of the GNU 'libC'
docs:
// note this is 'C' code.
// for 'C++', change the name 'string' to something else.
Why? string is not a keyword.
Quote:
const char string[] = "words separated by spaces -- and, punctuation!";
const char delimiters[] = " .,;:!-";
[...]
Quote:
So, if the above does not answer your question, you'll have to repost and
wait for an expert with his/her roots in 'C' (or 'strtok' experience).
If you were asking about the escape char (\), I don't know how 'strtok'
handles it. Experiment.
strtok doesn't handle escape characters.
Quote:
If you are going to code in 'C++', you really should learn std::string at a
minimum.
*sign*

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Thomas J. Gritzan
Guest
 
Posts: n/a
#11: Jun 6 '07

re: strtok as string replace function problem ?


Lothar Behrens wrote:
Quote:
On 6 Jun., 01:12, "BobR" <removeBadB...@worldnet.att.netwrote:
Quote:
>Lothar Behrens <lothar.behr...@lollisoft.dewrote in message ...
>>
>Please read this before your next post:http://www.parashift.com/c++-faq-lite/how-to-post.html
>>
>
Thank you all.
>
I'll take a look at std::string by searching in the samples of Open
Watcom for it.
>
BTW. I have fixed the problem if I have understood the strtok function
correctly.
>
Can the token be a 'multi character' string such as "\\\"" ?
>
(Not tokenizing '\\' and \"')
>
Maybe this would be a problem in my replace implementation.
Yes. strtok handles the delimiter string as collection of single chars. If
you want to replace substrings by strings, you would have to write another
implementation, perhaps searching the toReplace string with strstr and
replacing it.

Perhaps you get better answers in comp.lang.c for questions about the C
standard library (the people there are used to it).

--
Thomas
http://www.netmeister.org/news/learn2quote.html
BobR
Guest
 
Posts: n/a
#12: Jun 6 '07

re: strtok as string replace function problem ?



Thomas J. Gritzan wrote in message...
Quote:
BobR wrote:
Quote:
Lothar Behrens <lothar.behrens@lollisoft.dewrote in message...
Quote:
Can the token be a 'multi character' string such as "\\\"" ?
(Not tokenizing '\\' and \"')
Do you mean the 'delimiter'? Here is an example right out of the GNU
'libC'
Quote:
Quote:
docs:
// note this is 'C' code.
// for 'C++', change the name 'string' to something else.
>
Why? string is not a keyword.
It IS in newbie code!!! <G( using namespace std; )
Quote:
>
[...]
Quote:
If you were asking about the escape char (\), I don't know how 'strtok'
handles it. Experiment.
>
strtok doesn't handle escape characters.
Thanks for the info.

--
Bob R
POVrookie


Closed Thread


Similar C / C++ bytes