Answer:
This program is written in C++ language using Dev C++. The explanation of code is given below:
Explanation:
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;// user enter sentence
string find;//find word in sentence
string replace;//replace word in sentence
cout<<"Enter Sentence\n";
getline(cin, str);//user enter sentence
cout<<"\n Now enter key to find in sentence\n";
getline(cin,find);//user enter "word" to find in sentence
int findSize=(int)find.size();
cout<<"\nNow enter a replacing word\n";
getline(cin,replace);//user enter "word" to replace in sentence
cout << "\nOriginal text: " << str;//show original sentence
for (int j = 0; j < (int)str.size(); j++) {//loop
string key = str.substr(j, findSize), repl; //search in sentence
if (key == find) {//if find
repl = replace;//replace
for (int k = 0; k < findSize+2; k++) {//append
str[j+k] = repl[k];// new sentence
}
}
}
cout <<"\nNew text: " << str << endl;
return 0;
}