In: Computer Science
Python code:
def wordCount(string):
li = string.split(" ")
return len(li)
def mostFrequentWord(string):
li = string.split(" ")
s = list(set(li))
frequency = li.count(s[0])
ind = 0
for i in range(1,len(s)):
if li.count(s[i]) > frequency:
frequency = li.count(s[i])
ind = i
return s[ind]
def replaceWord(string1,string2,string3):
li = string1.split(" ")
s1 = " "
for i in range(len(li)):
if(li[i]==string2):
li[i] = string3
return s1.join(li)
def main():
string = input("Enter a sentence :")
cwords = wordCount(string)
print("There are "+str(cwords)+" words in the sentence")
print("The most frequent word in the sentence is :
"+mostFrequentWord(string))
print("Enter old string,then new string you want to
replace,seperate them with a space :")
temp = input().split(" ")
string2 = temp[0]
string3 = temp[1]
print("The new sentence after replacement is
:"+replaceWord(string,string2,string3))
if __name__ =="__main__":
main()
Execution screenshots:
Note:please like the
answer.Thank you.Have a nice day.