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

This is a Python Program to determine how many times a given letter occurs in a string recursively.

Problem Description

The program takes a string and determines how many times a given letter occurs in a string recursively.

Problem Solution

1. Take a string and a character from the user and store it in different variables.
2. Pass the string and the characters as arguments to a recursive function.
3. Pass the base condition that the string isn’t empty.
4. Check if the first character of the string is equal to the character taken from the user and if it is equal, increment the count.
5. Progress the string either wise and print the number of times the letter occurs in the string.
6. Exit.

Program/Source Code

Here is source code of the Python Program to determine how many times a given letter occurs in a string recursively. The program output is also shown below.

def check(string,ch):
      if not string:
        return 0
      elif string[0]==ch:
            return 1+check(string[1:],ch)
      else:
            return check(string[1:],ch)
string=raw_input("Enter string:")
ch=raw_input("Enter character to check:")
print("Count is:")
print(check(string,ch))
Program Explanation

1. User must enter a string and a character and store it in separate variables.
2. The string and the character is passed as arguments to the recursive function.
3. The base condition defined is that the string isn’t empty.
4. If the first character of the string is equal to the character taken from the user, the count is incremented.
5. The string is progressed by passing it recursively back to the function.
6. The number of times the letter is encountered in the string is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter string:abcdab
Enter character to check:b
Count is:
2
 
Case 2:
Enter string:hello world
Enter character to check:l
Count is:
3

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.