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
4
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: "))))
Similar questions