Using strings

#include <iostream>
#include <string>

using namespace std;

string replaceSubstring(string, string, string);

int main()
	string string1 = "the dog jumped over the fence",
			string2 = "the",
			string3 = "that",

	cout << "This program will replace a keyword in a sentence with another word.\n\n\n";
	cout << "Our sentence: " << string1 << endl;
	cout << "Replace \"" << string2 << "\" with \"" << string3 << "\"\n\n" << endl;

	cout << "Calling function ...\n\n";

	newString = replaceSubstring(string1, string2, string3);

	cout << newString << endl;

	return 0;

string replaceSubstring(string string1, string string2, string string3)
	int position = 0;
	int origLength = string2.length();

	position = string1.find(string2,position);

	while(position >= 0) {
		string1.erase(position, origLength);
		string1.insert(position, string3 );
		position = string1.find(string2, position);

	return string1;

Now I'm attempting to do this via a char array and kinda of stuck on the process. Obviously, declare the char arrays, allow a pointer to be returned from the function. My problem is I'm more than likely over complicating the search and replace. Would I need to dynamically allocate a new array and strcpy? Looking for thoughts and suggestions on how to tackle.

5 Years
Discussion Span
Last Post by mzimmers

My first thought is, why would you want to use arrays instead of strings? You have a perfectly good implementation using strings...why argue with prosperity?

If you do want to use arrays, you'll have to buffer the input string (the "sentence") and rebuild it into another array as you replace your words. Definitely not a better solution than what you already have. Plus, if the new array is defined in the function replaceSubstring, remember that it will disappear when your function exits, making it hard for the calling function (main()) to access it.

You're making good use of the tools given you...I don't see a reason for you to step backwards in time.


OK...well, as I said, you'll have to build the new string yourself. You'll be using substr() and strlen(). Give it a try, post the results and we'll go from there.


pass to function
strlen on original - 1 (null term)
loop thru char array index with an if
if index0 and array0 are = then
if index1 and array1 are = then
if index2 and array2 are = then
int1 = index
then iterate from index+3 til \0
strlen = replacement word
allocate new array
strcpy til int1 then replace word then int2


there's got to be an easier way...


To use char arrays, you'll need to pass four parameters to your replace function:

1. the char array you want to process
2. a char array with the word you want replaced
3. a char array with the word you want to replace #2 with
3 a new array with the altered string (string in the C sense).

while (strstr() != NULL) {
move the portion of the string prior to the found string into string #4
move the replacement string into string 4
advance a pointer past the occurrence of the found word

That's pseudo-code, of course. I'm sure you can code it up. Remember, you'll need two indices into string 1: one for the starting position of what to copy, and one for the ending position.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.