Sunday, 12 January 2025

Day 89: Python Program to Check whether a String is Palindrome or not using Recursion

 


f is_palindrome_recursive(s):

    if len(s) <= 1:

        return True

     if s[0] != s[-1]:

        return False

    return is_palindrome_recursive(s[1:-1])

string = input("Enter a string: ")

processed_string = string.replace(" ", "").lower()

if is_palindrome_recursive(processed_string):

    print(f'"{string}" is a palindrome.')

else:

    print(f'"{string}" is not a palindrome.')

#source code --> clcoding.com 

Code Explanation:

def is_palindrome_recursive(s):
This line defines a function named is_palindrome_recursive, which will check whether a given string s is a palindrome.
A palindrome is a word, phrase, or sequence that reads the same forward and backward (ignoring spaces and capitalization).

    if len(s) <= 1:
        return True
This line checks if the length of the string s is less than or equal to 1.
If the string is empty or has only one character, it is trivially a palindrome, so the function returns True.
Example: "a", "b", or an empty string "" are all palindromes.

    if s[0] != s[-1]:
        return False
This checks if the first character (s[0]) is not equal to the last character (s[-1]).
If the first and last characters are not the same, the string cannot be a palindrome, so it returns False.

    return is_palindrome_recursive(s[1:-1])
If the first and last characters are the same, the function makes a recursive call with a substring that excludes the first and last characters (s[1:-1]).
This means the function will continue checking the inner characters of the string, ignoring the outer characters.
The recursion keeps narrowing the string until it either finds a mismatch (and returns False), or the string becomes too short to continue (and returns True).

Input Handling
string = input("Enter a string: ")
This line prompts the user to enter a string. The input is stored in the variable string.

processed_string = string.replace(" ", "").lower()
This line processes the string by removing any spaces with replace(" ", "") and converting all characters to lowercase using lower().
This ensures that the palindrome check is case-insensitive and doesn't consider spaces.

if is_palindrome_recursive(processed_string):
    print(f'"{string}" is a palindrome.')
else:
    print(f'"{string}" is not a palindrome.')
The if statement calls the is_palindrome_recursive function, passing the processed string (processed_string), which has no spaces and is in lowercase.
If the function returns True, it prints that the original string (string) is a palindrome.
If the function returns False, it prints that the original string is not a palindrome.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (87) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (179) C (77) C# (12) C++ (83) Course (67) Coursera (231) Cybersecurity (24) Data Analytics (1) data management (11) Data Science (132) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Euron (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (4) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (62) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (958) Python Coding Challenge (398) Python Quiz (54) 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