Write a Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively

Given a string and a letter as input to the Python function. The python function should call itself recursively and find the number of times the letter has occurred inside the string.

We will be using only just three comparisons to find the count of letter occurrences in a string.

 

 

Write a Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively

 




Write a Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively


Define a function name as find_char_count()  pass the input argument as a string and a letter to it.

Take any if else block and compare the string 3 times.
Check if the string is empty return 0
Check using elif the first character of the string is a letter its return plus one and call the recursive function again,  by removing the first character from the string.
In the else block as we know the first character  string is not a letter at zero and call the recursive function I get by removing the first character from the string.

def find_char_count(text, letter):
    if not text:
        return 0
    elif text[0] == letter:
        return 1 + find_char_count(text[1:], letter)
    else:
        return 0 + find_char_count(text[1:], letter)





In the main program, take a string variable data  then I ask a user to enter a string using input function call. Take another character variable and ask a user to enter a character using input function call.


data = input("Enter a String : ")
my_char = input("Enter a Letter : ")





Take the result variable and call the recursive function by passing the string and character as an input to the above Python function. Using the print function, call print the result that is the count of frequency of letters occurring in the string.

result = find_char_count(data, my_char)
print(f"Letter {my_char} occurs {result} times")



Python Code
========================


def find_char_count(text, letter):
    if not text:
        return 0
    elif text[0] == letter:
        return 1 + find_char_count(text[1:], letter)
    else:
        return 0 + find_char_count(text[1:], letter)


data = input("Enter a String : ")
my_char = input("Enter a Letter : ")
result = find_char_count(data, my_char)
print(f"Letter {my_char} occurs {result} times")
 

 

OUTPUT
====================


Enter a String : hello world
Enter a Letter : l
Letter l occurs 3 times

 

 

OUTPUT
====================


Enter a String : ccba
Enter a Letter : c
Letter c occurs 2 times




OUTPUT
====================


Enter a String : abcd
Enter a Letter : z
Letter z occurs 0 times
 



OUTPUT
=================


Enter a String :
Enter a Letter : a
Letter a occurs 0 times





Conclusion
================

Execute the program by providing the Random string and letter as input. Note down the results.


Comment it down below if you have any suggestions to improve the program

Post a Comment

0 Comments