Thursday, 2 January 2025

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

 


def count_letter_recursively(s, letter):

    if not s:

        return 0

    return (1 if s[0] == letter else 0) + count_letter_recursively(s[1:], letter)

input_string = input("Enter a string: ")

input_letter = input("Enter the letter to count: ")

if len(input_letter) != 1:

    print("Please enter only one letter.")

else:

    count = count_letter_recursively(input_string, input_letter)

    print(f"The letter '{input_letter}' occurs {count} times in the string.")

#source code --> clcoding.com 

Code Explanation:

Function Definition

def count_letter_recursively(s, letter):

Purpose: Defines a function named count_letter_recursively.

Parameters:

s: A string in which the letter is counted.

letter: The specific letter to count within the string.

    if not s:

        return 0

Logic: Checks if the string s is empty (base case of recursion).

If s is empty, it returns 0 because there are no letters left to count.

    return (1 if s[0] == letter else 0) + count_letter_recursively(s[1:], letter)

Logic: Processes the first character of the string (s[0]):

If s[0] matches the letter, it adds 1 to the count.

Otherwise, adds 0.

Recursively calls count_letter_recursively on the rest of the string (s[1:]), effectively reducing the problem size by one character each time.

Input Handling

input_string = input("Enter a string: ")

Purpose: Prompts the user to enter a string and stores it in the variable input_string.

input_letter = input("Enter the letter to count: ")

Purpose: Prompts the user to enter the specific letter to count and stores it in input_letter.

Validation and Execution

if len(input_letter) != 1:

    print("Please enter only one letter.")

Logic: Checks if the user entered more than one character as the letter.

If input_letter is not exactly one character long, it prints an error message and exits.

else:

    count = count_letter_recursively(input_string, input_letter)

Logic: If the input is valid, it calls count_letter_recursively with the input_string and input_letter.

Result: The result of the function (number of occurrences) is stored in the variable count.

    print(f"The letter '{input_letter}' occurs {count} times in the string.")

Purpose: Prints the result, displaying how many times the letter appears in the input string.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (76) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (174) C (77) C# (12) C++ (82) Course (67) Coursera (231) Cybersecurity (24) data management (11) Data Science (129) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (61) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) Python (950) Python Coding Challenge (392) Python Quiz (46) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses