brel-logo-white brel-logo-white
  • Travel
  • Case Study
  • How to
  • Technology
  • More
    • Sports
    • Health
    • Articles
    • Review
Notification
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
BreldigitalBreldigital
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
Search
  • Quick Access
    • Home
    • History
    • My Saves
    • My Interests
    • My Feed
  • Categories
    • Travel
    • Sports
    • Health

Top Stories

Explore the latest updated news!

How many solutions exist for the given equation? 3x + 13 = 3(x + 6) + 1 zero one two infinitely many

What is a best practice when approaching an automation effort

How does using mynav allow accenture to help its clients become more sustainable?

Stay Connected

Find us on socials
248.1k Followers Like
61.1k Followers Follow
165k Subscribers Subscribe
Breldigital > Blog > ds > Count characters in string recursion
ds

Count characters in string recursion

By John Published November 10, 2022
Share

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))

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

TAGGED: Count characters in string recursion
John November 10, 2022 November 10, 2022

Search

brel-logo-white brel-logo-white
Explore a wide range of brands and categories with our comprehensive coverage, and stay up-to-date with the latest news and trends by subscribing to our updates.
Categories:
  • Entertainment
  • Travel
  • Sport
  • Contact Us

Quick Links

  • My Feed
  • My Interests
  • History
  • My Saves

About US

  • Adverts
  • Our Jobs
  • Term of Use

© 2023 Brel Digital All Rights Reserved. All logos and images used on this website are registered trademarks of their respective companies

Welcome Back!

Sign in to your account

Lost your password?