write a program to print frequency of each word
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))
Comments
Leave a comment