Write a Python program as a function that counts the number of times a character appears in a string without using any predefined functions.
def caracter_count(s , c):
# initializing counter
counter = 0
for x in s:
if c == x:
counter = counter + 1
# Testing function
s = "python programming"
print("The number of times of appearance of character 'n' in 's' is = " ,caracter_count(s , 'n'))
# The output is : The number of times of appearance of character 'n' in 's' is = 2
One thought on “Solution Exrcise 49: count number of times a character appears in string”
Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have. word counter