Given a sentence S, write a program to print the frequency of each word in S. The output order should correspond with the word's input order of appearance.
Answers
Answer:
Expert's answer
InputString = str(input("Enter a string: "))
str = InputString.split()
tempStr=[]
for i in str:
if i not in tempStr:
tempStr.append(i)
print("Word Frequency")
for i in range(0, len(tempStr)):
print("%10s:%5s"%(tempStr[i].ljust(20),str.count(tempStr[i])))
Python Output:
Enter a string: Given a sentence S, write a program to print the frequency of each word in S. The output order should correspond with the word's input order of appearance.
Word Frequency
Given : 1
a : 2
sentence : 1
S, : 1
write : 1
program : 1
to : 1
print : 1
the : 2
frequency : 1
of : 2
each : 1
word : 1
in : 1
S. : 1
The : 1
output : 1
order : 2
should : 1
correspond : 1
with : 1
word's : 1
input : 1
appearance. : 1