Write a PYTHON RECURSIVE function to count how many times a

Write a PYTHON RECURSIVE function to count how many times a character in a string.

>>> numberofcharacters(\'a,\'america\')

2

>>> numberofcharacters(\'e,\'engineering\')

3

Solution

Please follow the code and comments for description :

CODE :

srcStr = \'america\' # required values
varChar = \'a\'


def numberofcharacters(srcStr, varChar): # function that is called recursively to count the character count
   foundCount = 0 # temporary variable that stores the count
   for key in srcStr: # iterating over the loop to get the count value
       if key == varChar: # checking for the condition if the characters are same
           foundCount += 1 # incrementing the count value
   return foundCount # returning the count value
numberofcharacters(srcStr, varChar) # calling the functions recursively
print \'Total Count of %s is: %s \' %(varChar, numberofcharacters(srcStr, varChar)) # printing the resultant count

OUTPUT :

Total Count of a is: 2

Hope this is helpful.

Write a PYTHON RECURSIVE function to count how many times a character in a string. >>> numberofcharacters(\'a,\'america\') 2 >>> numberofchara

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site