Respuesta :
Answer:
sentence=input("Enter a sentence:")
lis= sentence.split();
print(lis)
word=input("Enter the word to replace:")
word1=input("Enter the word to be replaced:")
i=0
for i in range(0, len(lis)):
if lis[i]==word:
lis[i]=word1
i+=1
else:
i+=1
continue
print("Sentence after replacement is")
print(*lis)
Explanation:
The code is as above. We here are splitting the sentence and storing in a list. And then we are replacing the word, and finally printing the sentence with replaced word.
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;
}
