Westonci.ca connects you with experts who provide insightful answers to your questions. Join us today and start learning! Join our platform to get reliable answers to your questions from a knowledgeable community of experts. Discover detailed answers to your questions from a wide network of experts on our comprehensive Q&A platform.

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 the sentence where any word on the original list is replaced. Ex: If the input is: automobile car manufacturer maker children kids The automobile manufacturer recommends car seats for children if the automobile doesn't already have one.

Sagot :

Answer:

In Python:

toreplace = input("Word and replacement: ")

sentence = input("Sentence: ")

wordsplit = toreplace.split(" ")

for i in range(0,len(wordsplit),2):

   sentence = sentence.replace(wordsplit[i], wordsplit[i+1])

print(sentence)

Explanation:

This gets each word and replacement from the user

toreplace = input("Word and replacement: ")

This gets the sentence from the user

sentence = input("Sentence: ")

This splits the word and replacement using split()

wordsplit = toreplace.split(" ")

This iterates through the split words

for i in range(0,len(wordsplit),2):

Each word is then replaced by its replacement in the sentence

   sentence = sentence.replace(wordsplit[i], wordsplit[i+1])

This prints the new sentence

print(sentence)

Thanks for using our service. We're always here to provide accurate and up-to-date answers to all your queries. We appreciate your time. Please revisit us for more reliable answers to any questions you may have. Get the answers you need at Westonci.ca. Stay informed by returning for our latest expert advice.