Skip to content

Commit 2ce1504

Browse files
Program 15
1 parent 92017b4 commit 2ce1504

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Lab Cycle 2/program15.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Program to Count the number of characters (character frequency) in a string.
2+
3+
# Function to count the character frequency in a string
4+
def count(str):
5+
dict = {} # Dictionary to store the character frequency
6+
for n in str: # Loop to iterate through the string
7+
keys = dict.keys() # Get the keys of the dictionary
8+
if n in keys: # If the character is already present in the dictionary
9+
dict[n] += 1 # Increment the value of the character
10+
else:
11+
dict[n] = 1 # Else, add the character to the dictionary
12+
return dict # Return the dictionary
13+
14+
print("Enter a string: ")
15+
str = input()
16+
print("Character frequency in string",str,"is",count(str))

0 commit comments

Comments
 (0)