Python-courses-python-exercises-games-Python-courses-

Exercise 49

Write a Python program as a function that counts the number of times a character appears in a string without using any predefined functions.

Solution

def caracter_count(s , c):
# initializing counter
counter = 0
for x in s:
if c == x:
counter = counter + 1
return counter

# 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

Younes Derfoufi
my-courses.net
One thought on “Solution Exrcise 49: count number of times a character appears in string”

Leave a Reply