Your changes are path dependent meaning that a previous change could invalidate later matches that would have been found if the original string was unchanged. One way to avoid this is to use std::set:
#include <iostream>
#include <string>
#include <map>
constexprauto changes = 3;//change as required
int main()
{
std::cout << "Enter input string \n";
std::string input{};
getline(std::cin, input);
std::string input_copy{input};//keep a copy since original string will be changed shortly
std::map<std::string,std::string> replacements{};//std::unordered::map would make changes path-dependent
int i{};
while (i < changes)
{
std::cout << "Enter a character or phrase to be replaced: \n";
std::string original{};//use string since it can be character or phrase
getline(std::cin, original);
std::cout << "Enter the replacement character or phrase: \n";
std::string replacement{};
getline(std::cin, replacement);
replacements[original] = replacement;
++i;
}
for (constauto& elem : replacements)
{
std::string::size_type index = 0;
while (index != std::string::npos)
{
index = input.find(elem.first, index);
if(index == std::string::npos)
{
break;
}
else
{
input.replace(index, elem.first.size(), elem.second);
}
index += elem.first.size();
}
}
std::cout << "Original string: \n";
std::cout << input_copy << '\n';
std::cout << "Replaced string: \n";
std::cout << input << '\n';
}
/*Sample Output:
Enter input string
how are you today
Enter a character or phrase to be replaced:
o
Enter the replacement character or phrase:
A
Enter a character or phrase to be replaced:
re
Enter the replacement character or phrase:
ER
Enter a character or phrase to be replaced:
to
Enter the replacement character or phrase:
A7
Original string:
how are you today
Replaced string:
hAw aER yAu tAday//note the 'to' of 'today' is no longer picked up due to the first change*/
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
string msg = "";
string del = "";
string rep = "";
int pos = 0;
cout << "Enter a message: ";
getline( cin, msg );
cout << endl;
cout << "Enter a character or phrase to be replaced: ";
getline( cin, del );
cout << "Enter the replacement character or phrase: ";
getline( cin, rep );
int delLength = del.size(); // store size of deleted part
int repLength = rep.size(); // store size of replacement
while ( pos < msg.size() )
{
pos = msg.find( del, pos );
if ( pos == string::npos ) break;
msg.replace( pos, delLength, rep );
pos += repLength;
}
cout << "The message is: " << msg;
}
Enter a message: Hi Sir
Enter a character or phrase to be replaced: i
Enter the replacement character or phrase: z
The message is: Hz Szr
Enter a message: Tweedledum and Tweedledee agreed to have a battle; for Tweedledum said Tweedledee had spoiled his nice new rattle.
Enter a character or phrase to be replaced: e
Enter the replacement character or phrase: oo
The message is: Twoooodloodum and Twoooodloodoooo agrooood to havoo a battloo; for Twoooodloodum said Twoooodloodoooo had spoilood his nicoo noow rattloo.