Replace a word in string

So I have this function

string replace_sentence(string s)

{

s = string(s.begin(), s.end());

if (s.find("hate") != std::string::npos)
{

///replace "hate" with "love"
}
}

I need some help.

how to replace the word hate to love
Last edited on
What is that third line all about (s =)?

You may want to read the documentation for string.find() and perhaps use the return value from that function. Then, to continue your thought, you may want to consider reading the documentation for the std::string class to see if there isn't a member function that "rings a bell".

it is long program the 3rd line is the line that read from the text file
and also there is the challenge that replace the word with if statement
This is the line I was referring to:

s = string(s.begin(), s.end());

If you would have used code tags in your post you may have been able to see that:

1
2
3
4
5
6
7
8
9
10
string replace_sentence(string s)
{
   s = string(s.begin(), s.end());

   if (s.find("hate") != std::string::npos)
   {

   ///replace "hate" with "love"
   }
}

there is the challenge that replace the word with if statement

That's why I suggested you find and read the documentation for that standard class. That class has quite a few member functions that may do just what you want.
Topic archived. No new replies allowed.