Ask Question

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 children kids manufacturer maker The automobile manufacturer recommends car seats for children if the automobile doesn't already have one.

then the output is:

The car maker recommends car seats for kids if the car doesn't already have one.

You can assume the original words are unique.

+2
Answers (1)
  1. 27 September, 14:28
    0
    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.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program that replaces words in a sentence. The input begins with word replacement pairs (original and replacement). The next line ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers