I am trying to replace "the" with "that". My original sentence is "the ball is under the car". I can successfully change the first "the". But when I try to change the second "the" string1 ends up being "that ball us under that".So it cuts off the world "car". What am I doing wrong? Any help will be greatly appreciated.

#include <iostream>
#include <string>

using namespace std;

void replaceSubstring(string, string, string);
int main() {//start main

	
	string string1 = "the ball is under the car         ";
	string string2 = "the";
	string string3 = "that";
	
	

	int x;
	cout << "The orginal strings consist of: " << endl; 
		
		cout << "String1 = " << string1 << endl;
		cout << "String2 = " << string2 << endl;
		cout << "String3 = " << string3 << endl;

		

		replaceSubstring(string1,string2, string3);
		
			cin >> x;
}//end main



void replaceSubstring(string string1, string string2, string string3) {
	
	
	
	 
	
	int spot1; 
	int spot2; 
	
	
	spot1 = string1.find(string2, 0); 
	spot2=  string1.find(string2, 3); 
	 
	
	
	string1.replace(spot1, (spot1 + 3), string3);
	
	cout << string1 << endl;
	

	string1.replace(spot2, (spot2 + 3), string3);
	
	cout << string1;
	

}

Your problem is really in replaceString.

replaceSubstring(string1,string2, string3);
}

void replaceSubstring(string string1, string string2, string string3) 
{
  int spot1; 
  int spot2; 

  spot1 = string1.find(string2, 0); 
  spot2=  string1.find(string2, 3);    // From original string 
  string1.replace(spot1, (spot1 + 3), string3);
  // Now original string is NOT orignal length:
  // NOTE: Replace(position, length_to_change , substitute_string)  
  string1.replace(spot2, (spot2 + 3), string3);
  cout << string1;
}

what is happening is that you find the position in the string for the first and the second word. BUT you replace the first word, first. That shortens the string, THEN, when you replace the string. So you are one character off.

Then you make your next mistake. The replace takes three variables (a) the position,
(b) the length to replace (c) the substituted string. You have incorrectly thought that it takes two positions, easy mistake to make, but it is just the length. therefore in your code you accidentally chop off all of the string. [In the first instance the position is 0, so 0+3 is still the length.]

you might want to look at something like this:

void replaceSubstring(string&,const string&,const string&);
int main() {
  string string1 = "the ball is under the car         ";
  string string2 = "the";
  string string3 = "that";
  cout << "The orginal strings consist of: "<<string1 << endl; 
  replaceSubstring(string1,string2, string3);
  cout << "The new string consist of: "<<string1 << endl; 
}

void replaceSubstring(string& SMain, 
		      const string& searchString,
		      const string& replaceString) 
{
  // This is because string defines the type to be returned 
  // from find. However, it is often a BIGGER type than
  // int, and find returns a value std::string::npos to means
  // no string found. which does not fit in int.
  string::size_type index;
  index = SMain.find(SMain); 
  while(index!=string::npos)
    {
      SMain.replace(index,searchString.size(),replaceString);
      // see if there are any more to do
      index = SMain.find(searchString); 
    } 
  return;
}

What I have done here is added references so that you modify string1 on the return.
You might not need that , and I have changed ALL locations of the search string, again you might not want to do that.

More sophistication is needed, in that loop to avoid issues of the search string being contained in the replace string etc. That you can add by keeping track of the size of both the replacement section, the position that you found the result and the search string. It is worth experimenting with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.