replaceSubstring function

The assignment is read as: Write a function named replaceSubstring. The function should accept three C-strings or string object arguments. Let's call them string1, string2, string3. It should search string1 for all occurences of string2. When it finds an occurrence of string2, it should replace it with string3. For example, suppose the three arguments have the following values:

string1: "the dog jumped over the fence"
string2: "the"
string3: "that"

With these three arguments, the function would returna string object with the value "that dog jumped over that fence". Demonstrate the function in a complete program.

Here's my code:

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
26
27
28
29
30
#include <iostream>
#include <cstring>
using namespace std;

// function prototype
string replaceSubstring(string &, string &, string &);

int main()
{
	string string1 = "the dog jumped over the fence.";
	string string2 = "the";
	string string3 = "that";
	cout << string1 << endl;
	replaceSubstring(string1, string2, string3);
	cout << string1 << endl;

	return 0;
}

string replaceSubstring(string &str1, string &str2, string &str3)
{
	int x;

	for (x = 0; x < str1.length(); x++)
	{
		str1.find(str2.length(), x);
		str2.swap(str3);
	}
	return str1;
}



Question: I did create the function but it refuses to find str2 then swap the values of str2 with str3. What did I do wrong?
Last edited on
i think you have two problem:

1) read this and you understand your problem:

http://www.cplusplus.com/reference/string/string/find/

2) pattern function replaceSubstring(...) ; ( because you not know well 'find' )
Last edited on
ar2007,.

I read 1 to understand the problem. The 2nd I do not undersatnd what you mean by that. I may not know well how to do find but when I ran the program, it ran the output as:

"thatttt dog jumped over the fence"

Here's my current code for replaceSubstring function:

1
2
3
4
5
6
7
8
9
string replaceSubstring(string &str1, string &str2, string &str3)
{
	for (int x = 0; x < sizeof(str1); x++)
	{
		str1.find(str2);
		str1.replace(0, str2.length(), str3);
	}
	cout << str1;
}
(1) Unless you are planning to return some copy (which you aren't) replaceSubstring() should be a void function; change this in both prototype and declaration.

(2) Your loop designator
for (int x = 0; x < sizeof(str1); x++)
serves no purpose. Remove it. You will need a loop (because of the possibility of multiple occurrences of str2), but more likely a while loop.

(3) You should use the .find() member function (as @ar2007 correctly pointed you to) to find or update the position of str2 within str1. If you set first:
int pos = 0;
then
pos = str1.find( str2, pos );
will start searching at pos (the latter argument to .find() ) and return the position of str2 as an updated value of pos. Your code at present does nothing with the result of .find().

(4) If pos is returned as string::npos, then no occurrence is found; job done, so return at this point.

(5) Once you have updated pos to the correct position for a replace, then you can use
.replace(). Your code is close with this, but the first argument should be pos, and not 0 - otherwise you will keep replacing the start of the string, irrespective of where str2 is. After replacing, you will need to advance pos by the length of str3.

(6) Keep looping until your .find() call returns string::npos - no further occurrences of str2. Read the link that @ar2007 pointed you to carefully.
OP: if you are able and/or willing to use std::regex then you can pop the following into the required function:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include <regex>

int main()
{
    std::string target = "the dog jumped over the fence";
    std::regex reg1("the");
    target = std::regex_replace(target, reg1, "that");
    std::cout << target << "\n";//prints "that dog jumped over that fence"
}

about regex_replace in particular: http://www.cplusplus.com/reference/regex/regex_replace/
about C++ regex in general: http://www.cplusplus.com/reference/regex/ECMAScript/

Topic archived. No new replies allowed.