Answer to Question #56109 in C++ for Rebekah

Question #56109
Hey, please help how can I replace underscore with characters and memorize all the guessed letters..
1
Expert's answer
2015-11-10T03:01:00-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Rebekah
10.11.15, 10:55

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

LATEST TUTORIALS
APPROVED BY CLIENTS