public class Palindrome2 { public static void main(String[] args) { String s = "racecar"; boolean ans; ans = isPalindrome(s); } public static boolean isPalindrome(String w) { if ( w.length() <= 1 ) { // base cases return true; } else { int lastPos = w.length() - 1; return (w.charAt(0) == w.charAt(lastPos)) && isPalindrome( w.substring(1, lastPos) ) ; } } }