We appreciate your visit to Write a program that replaces words in a sentence The input begins with word replacement pairs original and replacement The next line of input is. This page offers clear insights and highlights the essential aspects of the topic. Our goal is to provide a helpful and engaging learning experience. Explore the content and find the answers you need!
Answer :
The program is an illustration of loops and conditional statements.
Loops are used to perform repetitive operations, while conditional statements are statements whose execution is dependent on its truth value.
The program in Python where comments are used to explain each line is as follows:
#This gets input for the words to replace and their replacements
wordReplace = input()
#This splits the words using space
wordReplace = wordReplace.split()
#This gets input for the original word
wordOriginal = input()
#This iterates through each word and the replacements
for i in range(0, len(wordReplace), 2):
#This replaces all words with the corresponding replacement
if wordReplace[i] in wordOriginal:
wordOriginal = wordOriginal.replace(wordReplace[i], wordReplace[i+1])
#This prints the new string
print(wordOriginal)
Read more about similar programs at:
https://brainly.com/question/21306076
Thanks for taking the time to read Write a program that replaces words in a sentence The input begins with word replacement pairs original and replacement The next line of input is. We hope the insights shared have been valuable and enhanced your understanding of the topic. Don�t hesitate to browse our website for more informative and engaging content!
- Why do Businesses Exist Why does Starbucks Exist What Service does Starbucks Provide Really what is their product.
- The pattern of numbers below is an arithmetic sequence tex 14 24 34 44 54 ldots tex Which statement describes the recursive function used to..
- Morgan felt the need to streamline Edison Electric What changes did Morgan make.
Rewritten by : Barada
Answer:
Following are the program in the Python Programming Language
#set variable to input sentence to replace words
sen = input()
#split that sentence
sen = sen.split()
#set variable to input whole sentence
get = input()
#set the for loop to replace words
for i in range(0, len(sen), 2):
#check condition when words of sen is in get
if sen[i] in get:
#then, replace words
get = get.replace(sen[i], sen[i+1])
#print the whole string after replacing
print('\n',get)
Explanation:
Following are the description of the program.
- Set a variable 'sen' that get a sentence for replacing the word.
- Split that sentence by split() method and again store that sentence in the following variable.
- Set a variable 'get' that gets the whole sentence from the user.
- Set the for loop for replacing words then, set the if conditional statement that checks the condition that the variable 'sen' in the variable 'get' then replace the words.
- Finally, print the whole sentence after the replacement.