Answer to Question #258531 in Python for reshma

Question #258531

write a program to print frequency of each word


1
Expert's answer
2021-10-29T07:20:02-0400


SAMPLE PROGRAM OUTPUT






SOLUTION CODE

#Solution code for above Question written in python langauage

#prompt the user to enter a string containing the words
my_string = input("Enter a string: ")

#declare a set to add all the words in the string add a fullstop
my_set = {"."}
for word in my_string.split():
    word.strip()
    my_set.add(word)
#print the set to have a view of all words in your string
print(my_set)
for element in my_set:
    my_word_count = 0;
    for word in my_string.split():
        if word == element:
            my_word_count = my_word_count + 1;

    print("Frequency of the word "+element+" = "+str(my_word_count))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog