Removing duplicates and replacing them with a string
Hello, I have been trying to figure out how to remove multiple "XXXX" from a string and replace them with the word "Siberia" once.
Code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = "Luzer";
string name = "<name>";
string xxx = "X";
string nothing = "";
string lucky = "lucky";
string str2 = "Siberia";
string str3 = "un";
size_t pos1, pos2, pos3;
string longStr = "Congratulations Mrs. <name>, you and Mr. <name> are the lucky recipients of a trip for two to XXXXXX. Your trip to XXX is already scheduled";
cout<<"Original String: \n\n"<<longStr<<endl<<endl;
pos1=longStr.find(name);
while(pos1!=string::npos)
{
longStr.replace(pos1, name.size(), str1);
pos1=longStr.find(name, pos1+1);
}
pos2=longStr.find(lucky);
longStr.insert(pos2, str3);
pos3=longStr.find(xxx);
while(pos3!=string::npos)
{
longStr.replace(pos3, xxx.length(), str2);
pos3=longStr.find(xxx, pos3);
}
longStr.append(" for December.");
cout<<"\n"<<longStr<<"\n\n";
system("pause");
return 0;
}
this prints "Siberia" in every spot there is an "X". so "SiberiaSiberiaSib..." and not only once as intended. Any help would be much appreciated.