In: Computer Science
find how many times a letter appear in a string for
example yellowstone letter l appear two times
(using python)
#Python program count.py
#main method
def main():
#set name
name ="yellowstone"
#set letter
letter='l'
#print Name
print('Name :%s'%name)
#print letter
print('Letter: %c'%letter)
#calling count
c=count(name, letter)
#print c
print('count: %d'%c)
#Method count that takes name and letter
#as input arguments and returns number of times
#letter occurs in name
def count(name, letter):
counter=0
for i in name:
if letter==i:
counter+=1
return counter
#calling main method
main()
------------------------------------------------------------------------
Sample output:
>>>
Name :yellowstone
Letter: l
count: 2