Recursive function to check if a string is palindrome | A recursive JAVA program to check whether a given String is palindrome or not ; A recursive function that check a str ( s . . e ) is palindrome or not . ; If there is only one character ; If first and last characters do not match ; If there are more than two characters , check if middle substring is also palindrome or not . ; An empty string is considered as palindrome ; Driver Code
import java . io . * ; class GFG { static boolean isPalRec ( String str , int s , int e ) { if ( s == e ) return true ; if ( ( str . charAt ( s ) ) != ( str . charAt ( e ) ) ) return false ; if ( s < e + 1 ) return isPalRec ( str , s + 1 , e - 1 ) ; return true ; } static boolean isPalindrome ( String str ) { int n = str . length ( ) ; if ( n == 0 ) return true ; return isPalRec ( str , 0 , n - 1 ) ; } public static void main ( String args [ ] ) { String str = " geeg " ; if ( isPalindrome ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }