Ok so im making a program that finds and replaces a word in a string of words. the thing is the program has to know where the word is and how long it is without it being hard coded int the program. My program attempts to get the word to be replaced from the user, then get the length of the word and replace it but im not sure what im doing wrong.
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string str;
cout<<"Enter the line: "<<endl;
getline(cin,str);
string str2 ;
cout<<"Which word do you want to find from the above line: ";
cin >> str2;
size_t found;
string str3;
cout << "what do you want to replace it with?" << endl;
getline(cin, str3);
cin.get();
found = str.find(str2);
str.replace(str.find(str2), str2.length(), str3);
cout << str << endl;
cin.get();
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string str;
cout<<"Enter the line: "<<endl;
getline(cin,str);
string str2 ;
cout<<"Which word you want to find from the above line: ";
cin>>str2;
size_t found;
found=str.find(str2);
cout<<"Enter a word: ";
string str3;
cin.ignore();
getline(cin,str3);
cout<<"After replacing with the given word: "<<endl;
str.replace(str.find(str2),str2.length(),str3);
cout << str << endl;
system("pause");
return 0;
}