Recursive problem solving palindrome
If String is just a character or a 0, it is a palindrome
Otherwise the string comparison of the first and last character
If the first and last character different, then it is not a palindrome
Otherwise, strip off the first and last characters, and then compare the rest of the string
1 public class palindrome { 2 public static void main(String[] args) 3 { 4 5 System.out.println(isPalindrome("rotor")); 6 7 } 8 9 static String lastCharacter(String str) 10 { 11 return str.substring(str.length()-1,str.length()); 12 } 13 14 static String firstCharacter(String str) 15 { 16 return str.substring(0,1); 17 } 18 19 static String middleCharacter(String str) 20 { 21 return str.substring(1,str.length()-1); 22 } 23 24 static String isPalindrome(String str) 25 { 26 if(str.length()==1 || str.length()==0) 27 { 28 return "equal"; 29 } 30 else 31 { 32 if(firstCharacter(str).equals(lastCharacter(str))) 33 { 34 return isPalindrome(middleCharacter(str)); 35 } 36 } 37 return "not equal"; 38 } 39 40 }