Q) Write a program to count frequency of characters in given string?
Sample Input:--
Enter a string : python programming
Output:-
a 1
g 2
h 1
i 1
m 2
n 2
o 2
p 2
r 2
t 1
y 1
python code:-
s=raw_input("Enter a stirng : ")
al='abcdefghijklmnopqrstuvwxyz'
au='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
l=[0]*26
m=[0]*26
for i in range(26):
l[i]=s.count(al[i])
m[i]=s.count(au[i])
for i in range(26):
if l[i]!=0:
print al[i],l[i]
if m[i]!=0:
print au[i],m[i]
Sample Input:--
Enter a string : python programming
Output:-
a 1
g 2
h 1
i 1
m 2
n 2
o 2
p 2
r 2
t 1
y 1
python code:-
s=raw_input("Enter a stirng : ")
al='abcdefghijklmnopqrstuvwxyz'
au='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
l=[0]*26
m=[0]*26
for i in range(26):
l[i]=s.count(al[i])
m[i]=s.count(au[i])
for i in range(26):
if l[i]!=0:
print al[i],l[i]
if m[i]!=0:
print au[i],m[i]
Very informative article.Thank you author for posting this kind of article .
ReplyDeletehttp://www.wikitechy.com/view-article/c-program-to-find-frequency-of-characters-in-a-string-with-example-and-explanation
Both are really good,
Cheers,
Venkat