We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 92017b4 commit 2ce1504Copy full SHA for 2ce1504
Lab Cycle 2/program15.py
@@ -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