Program to Check if Given String is Palindrome or Not using Recursion

Program to Check if Given String is Palindrome or Not using Recursion

  • Write a program to Check if Given String is Palindrome or Not using Recursion in C
  • Write a program to Check if Given String is Palindrome or Not using Recursion in C++
  • Write a program to Check if Given String is Palindrome or Not using Recursion in Python
  • Write a program to Check if Given String is Palindrome or Not using Recursion in PHP
  • Write a program to Check if Given String is Palindrome or Not using Recursion in Java
  • Write a program to Check if Given String is Palindrome or Not using Recursion in Java Script
  • Write a program to Check if Given String is Palindrome or Not using Recursion in C#

Explanation:

Using recursion, we compare the string’s start and end characters to determine whether it is a palindrome. We recursively inspect the remainder of the string (apart from the initial and last characters) to see if they are the same. The string is not a palindrome if the characters don’t match at any point.

Steps:

  1. Base Case:
    • If the string is empty or has only one character, it’s automatically a palindrome.
  2. Recursive Case:
    • Compare the first character and the last character of the string.
    • If they match, check the remaining substring (i.e., remove the first and last characters and call the function recursively).
    • If at any point the characters don’t match, the string is not a palindrome, and return False.
  3. Stopping Condition:
    • The recursion stops when the string has only one character or is empty, which indicates the string is a palindrome.

Program to Check if Given String is Palindrome or Not using Recursion

#include <stdio.h>
#include <string.h>

int check_palindrome(char str[], int start, int end) {
    if (start >= end)
        return 1;
    if (str[start] != str[end])
        return 0;
    return check_palindrome(str, start + 1, end - 1);
}

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove newline character if present
    str[strcspn(str, "\n")] = '\0';

    int length = strlen(str);
    if (check_palindrome(str, 0, length - 1))
        printf("'%s' is a palindrome.\n", str);
    else
        printf("'%s' is not a palindrome.\n", str);

    return 0;
}

#include <iostream>
#include <string>
using namespace std;

bool check_palindrome(string str, int start, int end) {
    if (start >= end)
        return true;
    if (str[start] != str[end])
        return false;
    return check_palindrome(str, start + 1, end - 1);
}

int main() {
    string str;
    cout << "Enter a string: ";
    getline(cin, str);

    if (check_palindrome(str, 0, str.length() - 1))
        cout << "'" << str << "' is a palindrome." << endl;
    else
        cout << "'" << str << "' is not a palindrome." << endl;

    return 0;
}

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-python">def check_palindrome(str, start, end):
    if start >= end:
        return True
    if str[start] != str[end]:
        return False
    return check_palindrome(str, start + 1, end - 1)

str = input("Enter a string: ")
if check_palindrome(str, 0, len(str) - 1):
    print(f"'{str}' is a palindrome.")
else:
    print(f"'{str}' is not a palindrome.")

<?php
function check_palindrome($str, $start, $end) {
    if ($start >= $end)
        return true;
    if ($str[$start] != $str[$end])
        return false;
    return check_palindrome($str, $start + 1, $end - 1);
}

echo "Enter a string: ";
$str = trim(fgets(STDIN));

if (check_palindrome($str, 0, strlen($str) - 1))
    echo "'$str' is a palindrome.\n";
else
    echo "'$str' is not a palindrome.\n";
?>

import java.util.Scanner;

public class Main {
    public static boolean check_palindrome(String str, int start, int end) {
        if (start >= end)
            return true;
        if (str.charAt(start) != str.charAt(end))
            return false;
        return check_palindrome(str, start + 1, end - 1);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String str = scanner.nextLine();

        if (check_palindrome(str, 0, str.length() - 1))
            System.out.println("'" + str + "' is a palindrome.");
        else
            System.out.println("'" + str + "' is not a palindrome.");
    }
}

function check_palindrome(str, start, end) {
    if (start >= end)
        return true;
    if (str[start] !== str[end])
        return false;
    return check_palindrome(str, start + 1, end - 1);
}

const str = prompt("Enter a string:");
if (check_palindrome(str, 0, str.length - 1))
    console.log(`'${str}' is a palindrome.`);
else
    console.log(`'${str}' is not a palindrome.`);

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-csharp">using System;

class Program {
    static bool CheckPalindrome(string str, int start, int end) {
        if (start >= end)
            return true;
        if (str[start] != str[end])
            return false;
        return CheckPalindrome(str, start + 1, end - 1);
    }

    static void Main() {
        Console.Write("Enter a string: ");
        string str = Console.ReadLine();

        if (CheckPalindrome(str, 0, str.Length - 1))
            Console.WriteLine($"'{str}' is a palindrome.");
        else
            Console.WriteLine($"'{str}' is not a palindrome.");
    }
}

List of All Programs