#include <iostream>
#include <String>
using namespace std;
void replace(string & str, const string & from, const string & to)
{
if(from.empty())
return;
int start_pos = 0;
while((start_pos = str.find(from, start_pos)) != str.npos)
{
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
}
int main()
{
string text, result;
cin>>text;
result = text;
replace(result, "_", " ");
cout<<"Orginal text: "<<text<<endl;
cout<<"Result text: "<<result<<endl;
system("pause");
return 0;
}
Comments
Thank you so much.. I really appreciate it. I'm sorry for not being specific at first. Actually, I have project and the project asked to do hangman game. Firstly, I have to compare the country name with the user guessed letter. But, at the same time I have to display the underscore to represent the hidden country name. Once the user enter one of the correct country name, then how can I replace the underscore with correct letter and correct position?
Leave a comment