Monday, 13 November 2017

Python program to get the frequency of characters in a string

C++ Program to calculate the frequency of the character of the given string is given below. In this Program we used python dictionaries where key is the character of string and its value is the occurrence of that character in the given string.

PROGRAM:

def calculate_frequency(input_string):
 freq=dict()
 for i in range(0,len(input_string)):
  if input_string[i] not in freq.keys():
   freq[input_string[i]]=1
  else:
   freq[input_string[i]]+=1
 return freq

 

print("\n\nEnter the string: \n")
input_string=str(input())
freq=calculate_frequency(input_string)
print("\nChar   Frequency:\n")
for key in freq.keys():
 print("%s        %s"%(key,freq[key]))


OUTPUT:



EmoticonEmoticon