- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively
When it is required to check the number of times a given letter occurs in a string using recursion, a method can be defined, and an ‘if’ condition can be used.
The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.
Below is a demonstration for the same −
def check_frequency(my_str,my_ch): if not my_str: return 0 elif my_str==my_ch: return 1+check_frequency(my_str[1:],my_ch) else: return check_frequency(my_str[1:],my_ch) my_string = input("Enter the string :") my_char = input("Enter the character that needs to be checked :") print("The frequency of " + str(my_char) + " is :") print(check_frequency(my_string,my_char))
Enter the string :jaanea Enter the character that needs to be checked :a The frequency of a is : 3
- A method named ‘check_frequency’ is defined that takes a string and a character as parameters.
- It checks to see if the characters in a string match the character passed to the method.
- If they do, it is returned.
- Else the method is called recursively on all characters of the string.
- The string and the character are taken as user inputs.
- The method is called by passing these values as parameters.
- The output is dislayed on the console.
- Python Program to Determine Whether a Given Number is Even or Odd Recursively
- Program to find how many times a character appears in a string in PHP
- Golang Program to Determine Recursively Whether a Given Number is Even or Odd
- Program to determine the minimum cost to build a given string in python
- Program to count how many times we can find "pizza" with given string characters in Python
- Python Program to Search the Number of Times a Particular Number Occurs in a List
- C++ program to concatenate a string given number of times?
- Get count of how many times a string appears in a MySQL column?
- Python - Check if k occurs atleast n times in a list
- Check if a string can become empty by recursively deleting a given sub-string in Python
- Find number of times every day occurs in a Year in Python
- Python Program to find out how many times the balls will collide in a circular tube
- Python program to determine whether the given number is a Harshad Number
- Write a function that counts the number of times a given int occurs in a Linked List in C++