Check if given String is palindrome using recursion

Problem statement:- Program to Check if the given String is palindrome or not using recursion.

Data requirement:-

   Input Data:- str[]

  Output Data:-
String output

Program in C

Here is the source code of the C program to check if the given string is palindrome or not using recursion.

Code:

#include<stdio.h>
#include<string.h>
int Check_Palindrome(char str[], int i, int len)
{
     static int count=0;
     if(i<strlen(str))
    if(str[i]==str[len])
    {
        count++;
        Check_Palindrome(str,i+1,len-1);
    }
    return count;
}
int main()
{
    char str[30];
    printf("Enter your String:");
   scanf("%[^\n]",str);
   if(Check_Palindrome(str,0,strlen(str)-1)==strlen(str))
    printf("It is a Palindrome String.");
   else
    printf("It is not a Palindrome String.");

}

Input/Output:
Enter your String:madam
It is a Palindrome String.

Program in C++

Here is the source code of the C++ Program to Program to Check if the given String is palindrome or not using recursion.

Code:

#include<iostream>
#include<cstring>
using namespace std;
int Check_Palindrome(char str[], int i, int len)
{
     static int c=0;
    if(i<strlen(str))
    if(str[i]==str[len])
    {
        c++;
        Check_Palindrome(str,i+1,len-1);
    }
    return c;

}
int main()
{
    char str[30];
    cout<<"Enter your String:";
   cin>>str;
   if(Check_Palindrome(str,0,strlen(str)-1)==strlen(str))
    cout<<"It is a Palindrome String.";
   else
    cout<<"It is not a Palindrome String.";

}

Input/Output:
Enter your String:abab
It is not a Palindrome String.

Program in Java

Here is the source code of the Java Program to Check if the given String is palindrome or not using recursion.

Code:

import java.util.Scanner;
public class Check_PalindromeString {
  int c=0;
int Check_Palindrome(char str[], int i, int len)
{
 
    if(i<str.length && len>=0)
    if(str[i]==str[len])
    {
        c++;
        //System.out.println(1);
        Check_Palindrome(str,i+1,len-1);
    }
    return c;
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
   System.out.print("Enter your String:");
   String str1=cs.nextLine();
   char[] str=str1.toCharArray();
   Check_PalindromeString  ob=new Check_PalindromeString();
   if(ob.Check_Palindrome(str,0,str.length-1)==str.length)
   System.out.print("It is a Palindrome String.");
   else
   System.out.print("It is not a Palindrome String.");
    cs.close();
}
}

Input/Output:
Enter your String:noon
It is a Palindrome String.

Program in Python

Here is the source code of the Python program to Program to Check if the given String is palindrome or not using recursion.

Code:

c=0
def Check_Palindrome(str,i,length):
    global c
    if (i < len(str)):
        if (str[i] == str[length]):
            c +=1
            Check_Palindrome(str, i + 1, length-1)
    return c
str=input("Enter your String:")
if(Check_Palindrome(str,0,len(str)-1)==len(str)):
    print("It is a Palindrome String.")
else:
print("It is not a Palindrome String.")

Input/Output:

Post a Comment

0 Comments