Problem with find for string objects

G

Gaijinco

I was trying a function that read an string (which previously had
attached two special characters & for the beginning of the word and #
for the end of the word) and if it was "<word>ize" it changed into
"change into <word>". I did this function:

bool Ize(string& s)
{
bool is=false;
string eval="ize";
int end=s.find('#');
int idx=s.rfind(eval,fin);
if(idx==end-eval.size())
{
is=true;
s.erase(idx,eval.size());
s.insert(s.find('&'),"change into ");
}
return is;
}

The problem I'm running into is with the string "z". For whatever the
reason s.rfind(eval,fin) returns -1 so it enters the if and when I try
to erase all hell breaks lose.

Why does it return -1 and not string::npos?

Thanks.
 
H

Heinz Ozwirk

Gaijinco said:
I was trying a function that read an string (which previously had
attached two special characters & for the beginning of the word and #
for the end of the word) and if it was "<word>ize" it changed into
"change into <word>". I did this function:

bool Ize(string& s)
{
bool is=false;
string eval="ize";
int end=s.find('#');
int idx=s.rfind(eval,fin);
if(idx==end-eval.size())
{
is=true;
s.erase(idx,eval.size());
s.insert(s.find('&'),"change into ");
}
return is;
}

The problem I'm running into is with the string "z". For whatever the
reason s.rfind(eval,fin) returns -1 so it enters the if and when I try
to erase all hell breaks lose.

Why does it return -1 and not string::npos?

-1 IS string::npos -- when you make the mistake to convert it to an int. The
result of string::find is string::size_type. If that's too much typing, you
could eventually use size_t, but NEVER int.

Heinz
 
D

Daniel T.

Gaijinco said:
I was trying a function that read an string (which previously had
attached two special characters & for the beginning of the word and #
for the end of the word) and if it was "<word>ize" it changed into
"change into <word>". I did this function:

bool Ize(string& s)
{
bool is=false;
string eval="ize";
int end=s.find('#');
int idx=s.rfind(eval,fin);
if(idx==end-eval.size())
{
is=true;
s.erase(idx,eval.size());
s.insert(s.find('&'),"change into ");
}
return is;
}

The problem I'm running into is with the string "z". For whatever the
reason s.rfind(eval,fin) returns -1 so it enters the if and when I try
to erase all hell breaks lose.

Why does it return -1 and not string::npos?

It doesn't return string::npos because you are converting it to an int,
and npos is not representable as an int on your system.

Neither "find" nor "rfind" return ints, they return string::size_type.
Even if you change the code though, I don't think it will fix your
problem. rfind is returning "-1" because the value doesn't exist in the
string.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top