Computer Science, asked by shruti13845, 5 months ago

consider the string as:blue bottle is in blue bag lying on blue carpet.
wap to accept a sample string and replace the word blue with red, bottle with glass and carpet with table as:
red glass is in red bag lying on red table

write a program in java

do not spam ​

Answers

Answered by jai696
4

\huge\red{\mid{\fbox{\tt{Using\: Python\: 3}}\mid}}

import re

from string import punctuation

def replacer(s):

replace = {

"blue": "red",

"bottle": "glass",

"carpet": "table"

}

new_words = []

for w in re.sub('['+punctuation+']', '', s).split():

new_words.append(replace[w]) if w in replace.keys() else new_words.append(w)

return " ".join(new_words) + "."

print(replacer((s := input("string: "))))

\large\mathsf\color{lightgreen}useful?\: \color{white}\longrightarrow\: \color{orange}brainliest!

Similar questions