A replace function.

Jun 22, 2008 at 8:00pm

Does anyone know where i can get a function that can take 3 strings and return a replaced version with all instances of the second string replaced by the third?

For example:

sring1="hello hello hello world."
string2="hello "
string3=Replace(string1,string2,"goodbye ");

string 3 should now be "goodbye goodbye goodbye world."


Any help would be greatly appreciated, i took a look at string::replace and it looked horribly complicated.

Last edited on Jun 22, 2008 at 8:00pm
Jun 22, 2008 at 8:56pm
Is this homework? (Because it sure looks like it)

The basic algorithm is:
1
2
3
4
5
6
7
8
s : string to modify
f : string to find
r : string to put in place of f

while (f is found in s)
  {
  replace f with r
  }

Hint: stay away from the iterator versions. Use the ones that take an index and a length. The very first one listed here should do it for you:
http://www.cplusplus.com/reference/string/string/replace.html

Hope this helps.
Jun 23, 2008 at 7:10am
Lol, no its not homework, im trying to convert some code from a basic language to C++

The basic has a function called "Replace" which takes 3 strings and does as i described. I looked at the string::replace but none of its forms can take the 3 stings and do the job. If i use a position and size then surely if the replacer is longer than the replace-ee then thats going to overwrite more than i want it too :/ Plus im not sure how to even get the position or length i need either.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>

using namespace std;


string Replace(string a,string b,string c)
{
        int pos = a.find(b);
        a.replace(pos, c.length(), c);

        return a;
}

int main()

{
    std::cout << Replace("  Hello World","Hello","goodbye");
}


This is my attempt but i am getting
goodbyeorld

As its simply overwriting rather than resizing a.

edit:
Ah ok fixed it, i needed to supply the length of b. Now i just need to make it parse the whole string :)

Is this the best way of doing this?
Last edited on Jun 23, 2008 at 7:51am
Jun 23, 2008 at 7:52am
You will need to write your own replace function to duplicate the one you had.

If you look at the example in the reference Duoas gave you will see that str.replace(9,5,str2) will replace 5 characters starting at character 9 with the contents of str2 - extending str as required.
1
2
3
4
string base="this is a test string.";
string str2="n example";
string str=base;                // "this is a test string."
str.replace(9,5,str2);          // "this is an example string." 


Try find and length from the strings library to complete the puzzle:-)

I'd advise quickly reading through the whole of the strings library reference (well, actualy the whole of the reference section) just to get an idea of what's there. Then the next time you have a problem you will have an idea what is directly possible, which is always a help.
Jun 23, 2008 at 8:03am
thanks :)

Well, here it is for anyone else who needs a basic replacer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;


string Replace(string a,string b,string c)
{
    int pos ;
    do
    {
        pos = a.find(b);
        if (pos!=-1)  a.replace(pos, b.length(), c);
    }
    while (pos!=-1);
    return a;
}

int main()

{
    std::cout << Replace("Hello World Hello","Hello","goodbye");
}


Thanks for the helps.
Last edited on Jun 23, 2008 at 8:04am
Topic archived. No new replies allowed.