The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Recursion:
Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.
Given a string and a character the task is to count the occurrence of the given letter in the string using recursive approach in Python.
Examples:
Example1:
Input:
Enter some random string= btechgeeks Enter some random character= e
Output:
Priting the count of e in the given string btechgeeks = 3
- Python Program to Reverse a String Using Recursion
- Python Program to Find the Least Frequent Character in a String
- Python Program to Find the Power of a Number Using Recursion
Example2:
Input:
Enter some random string= symphony Enter some random character= y
Output:
Printing the count of y in the given string symphony = 2
Program to Determine How Many Times a Given Letter Occurs in a String Recursively
Below are the ways to count the occurrence of the given letter in the string using recursive approach in Python.
Method #1:Using Recursion(Static Input)
Approach:
- Give the string and character as static input.
- Pass the string and the characters to a recursive function as arguments.
- Pass the fundamental constraint that the string is not empty.
- If the initial character of the string is the same as the character taken from the user, increment the count.
- The string is progressed in either direction, and the number of times the letter appears in the string is printed.
- Exit of program.
Below is the implementation:
# function which returns count of the given character in the string recursively. def checkCountRecursively(given_string, character): # Pass the fundamental constraint that the string is not empty. if not given_string: return 0 # If the initial character of the string is the same as the character # taken from the user, increment the count. elif given_string[0] == character: return 1+checkCountRecursively(given_string[1:], character) # The string is progressed in either direction, and the number of times # the letter appears in the string is printed. else: return checkCountRecursively(given_string[1:], character) # given string as static input given_string = 'btechgeeks' # given character as static input given_character = 'e' # passing the given character and given string to checkCountRecursively function print('Priting the count of', given_character, 'in the given string', given_string, '=', checkCountRecursively(given_string, given_character))
Output:
Priting the count of e in the given string btechgeeks = 3
Method #2:Using Recursion(User Input)
Approach:
- Scan the string and character as user input using input() function.
- Pass the string and the characters to a recursive function as arguments.
- Pass the fundamental constraint that the string is not empty.
- If the initial character of the string is the same as the character taken from the user, increment the count.
- The string is progressed in either direction, and the number of times the letter appears in the string is printed.
- Exit of program.
Below is the implementation:
# function which returns count of the given character in the string recursively. def checkCountRecursively(given_string, character): # Pass the fundamental constraint that the string is not empty. if not given_string: return 0 # If the initial character of the string is the same as the character # taken from the user, increment the count. elif given_string[0] == character: return 1+checkCountRecursively(given_string[1:], character) # The string is progressed in either direction, and the number of times # the letter appears in the string is printed. else: return checkCountRecursively(given_string[1:], character) # Scan some random string as user input using input() function. given_string = input('Enter some random string= ') # Scan some random character as user input using input() function. given_character = input('Enter some random character= ') # passing the given character and given string to checkCountRecursively function print('Printing the count of', given_character, 'in the given string', given_string, '=', checkCountRecursively(given_string, given_character))
Output:
Enter some random string= symphony Enter some random character= y Printing the count of y in the given string symphony = 2
Explanation:
- A string and a character must be entered by the user and stored in distinct variables.
- The recursive function is given the string and the character as arguments.
- The basic criterion is that the string is not empty.
- The count is increased if the first character of the string matches the character taken from the user.
- The string is updated by passing it back to the method recursively.
- The number of times the letter appears in the string is displayed.
Related Programs:
- Python Program to Compute all the Permutation of the String
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion
- Python Program to Remove the Characters of Odd Index Values in a String
- Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String
- Python Program to Calculate the Number of Digits and Letters in a String