Recursive function to check if a string is palindrome - GeeksforGeeks

05 Jan, 2022
Given a string, write a recursive function that checks if the given string is a palindrome, else, not a palindrome. 
Examples: 
Input : malayalam
Output : Yes
Reverse of malayalam is also
malayalam.

Input : max
Output : No
Reverse of max is not max. 
We have discussed an iterative function here. The idea of a recursive function is simple: 
1) If there is only one character in string
   return true.
2) Else compare first and last characters
   and recur for remaining substring.
Below is the implementation of the above idea: 
C++
C
Java
Python
C#
PHP
Javascript
// A recursive C++ program to// check whether a given number// is palindrome or not#include <bits/stdc++.h>using namespace std; // A recursive function that// check a str[s..e] is// palindrome or not.bool isPalRec(char str[],              int s, int e){         // If there is only one character    if (s == e)    return true;     // If first and last    // characters do not match    if (str[s] != str[e])    return false;     // If there are more than    // two characters, check if    // middle substring is also    // palindrome or not.    if (s < e + 1)    return isPalRec(str, s + 1, e - 1);     return true;} bool isPalindrome(char str[]){    int n = strlen(str);         // An empty string is    // considered as palindrome    if (n == 0)        return true;         return isPalRec(str, 0, n - 1);} // Driver Codeint main(){    char str[] = "geeg";     if (isPalindrome(str))    cout << "Yes";    else    cout << "No";     return 0;} // This code is contributed by shivanisinghss2110
// A recursive C program to// check whether a given number// is palindrome or not#include <stdio.h>#include <string.h>#include <stdbool.h> // A recursive function that// check a str[s..e] is// palindrome or not.bool isPalRec(char str[],              int s, int e){    // If there is only one character    if (s == e)    return true;     // If first and last    // characters do not match    if (str[s] != str[e])    return false;     // If there are more than    // two characters, check if    // middle substring is also    // palindrome or not.    if (s < e + 1)    return isPalRec(str, s + 1, e - 1);     return true;} bool isPalindrome(char str[]){int n = strlen(str); // An empty string is// considered as palindromeif (n == 0)    return true; return isPalRec(str, 0, n - 1);} // Driver Codeint main(){    char str[] = "geeg";     if (isPalindrome(str))    printf("Yes");    else    printf("No");     return 0;}
// A recursive JAVA program to// check whether a given String// is palindrome or notimport java.io.*; class GFG{    // A recursive function that    // check a str(s..e) is    // palindrome or not.    static boolean isPalRec(String str,                            int s, int e)    {        // If there is only one character        if (s == e)            return true;         // If first and last        // characters do not match        if ((str.charAt(s)) != (str.charAt(e)))            return false;         // If there are more than        // two characters, check if        // middle substring is also        // palindrome or not.        if (s < e + 1)            return isPalRec(str, s + 1, e - 1);         return true;    }     static boolean isPalindrome(String str)    {        int n = str.length();     // An empty string is    // considered as palindrome        if (n == 0)            return true;         return isPalRec(str, 0, n - 1);    }     // Driver Code    public static void main(String args[])    {        String str = "geeg";         if (isPalindrome(str))            System.out.println("Yes");        else            System.out.println("No");    }} // This code is contributed// by Nikita Tiwari
# A recursive Python program# to check whether a given# number is palindrome or not # A recursive function that# check a str[s..e] is# palindrome or not.def isPalRec(st, s, e) :         # If there is only one character    if (s == e):        return True     # If first and last    # characters do not match    if (st[s] != st[e]) :        return False     # If there are more than    # two characters, check if    # middle substring is also    # palindrome or not.    if (s < e + 1) :        return isPalRec(st, s + 1, e - 1);     return True def isPalindrome(st) :    n = len(st)         # An empty string is    # considered as palindrome    if (n == 0) :        return True         return isPalRec(st, 0, n - 1);  # Driver Codest = "geeg"if (isPalindrome(st)) :    print "Yes"else :    print "No"     # This code is contributed# by Nikita Tiwari.
// A recursive C# program to// check whether a given number// is palindrome or notusing System; class GFG{     // A recursive function that    // check a str(s..e)    // is palindrome or not.    static bool isPalRec(String str,                         int s,                         int e)    {                 // If there is only one character        if (s == e)            return true;         // If first and last character        // do not match        if ((str[s]) != (str[e]))            return false;         // If there are more than two        // characters, check if middle        // substring is also        // palindrome or not.        if (s < e + 1)            return isPalRec(str, s + 1,                            e - 1);             return true;    }     static bool isPalindrome(String str)    {        int n = str.Length;         // An empty string is considered        // as palindrome        if (n == 0)            return true;         return isPalRec(str, 0, n - 1);    }     // Driver Code    public static void Main()    {        String str = "geeg";         if (isPalindrome(str))            Console.Write("Yes");        else            Console.Write("No");    }} // This code is contributed by Nitin Mittal.
<?php// A recursive php program to// check whether a given number// is palindrome or not  // A recursive function that// check a str[s..e] is// palindrome or not.function isPalRec($str, $s,$e){    // If there is only one character    if ($s == $e)    return true;     // If first and last    // characters do not match    if ($str[$s] != $str[$e])    return false;     // If there are more than two    // characters, check if middle    // substring is also palindrome or not.    if ($s < $e + 1)    return isPalRec($str, $s + 1, $e - 1);     return true;} function isPalindrome($str){$n = strlen($str); // An empty string is// considered as palindromeif ($n == 0)    return true; return isPalRec($str, 0, $n - 1);} // Driver Code{    $str = "geeg";     if (isPalindrome($str))    echo("Yes");    else    echo("No");     return 0;} // This code is contributed// by nitin mittal.?>
<script>    // A recursive javascript program to    // check whether a given String    // is palindrome or not    // A recursive function that    // check a str(s..e) is    // palindrome or not.    function isPalRec( str , s , e) {        // If there is only one character        if (s == e)            return true;         // If first and last        // characters do not match        if ((str.charAt(s)) != (str.charAt(e)))            return false;         // If there are more than        // two characters, check if        // middle substring is also        // palindrome or not.        if (s < e + 1)            return isPalRec(str, s + 1, e - 1);         return true;    }     function isPalindrome( str) {        var n = str.length;         // An empty string is        // considered as palindrome        if (n == 0)            return true;         return isPalRec(str, 0, n - 1);    }     // Driver Code             var str = "geeg";         if (isPalindrome(str))            document.write("Yes");        else            document.write("No"); // This code contributed by gauravrajput1</script>
Yes
Another Approach :
Basically while traversing check whether ith and n-i-1th index are equal or not.
If there are not equal return false and if they are equal then continue with the recursion calls.
C++
#include <iostream>using namespace std; bool isPalindrome(string s, int i){            if(i > s.size()/2){       return true ;    }        return s[i] == s[s.size()-i-1] && isPalindrome(s, i+1) ;    }          int main(){    string str = "geeg" ;    if (isPalindrome(str, 0))    cout << "Yes";    else    cout << "No";     return 0; }
Yes
Time Complexity: O(n)
Auxiliary Space: O(n)
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
shivanisinghss2110
GauravRajput1
prasanna1995
palindrome
Strings
Strings
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Different methods to reverse a string in C/C++
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
Check whether two strings are anagram of each other
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Top 50 String Coding Problems for Interviews