// CSE 143, Winter 2010, Marty Stepp // This program uses recursion to decide whether strings are palindromes. public class Palindromes { public static void main(String[] args) { System.out.println(isPalindrome("madam")); System.out.println(isPalindrome("racecar")); System.out.println(isPalindrome("step on no pets")); System.out.println(isPalindrome("able was I ere I saw elba")); System.out.println(isPalindrome("Java")); System.out.println(isPalindrome("rotater")); System.out.println(isPalindrome("byebye")); System.out.println(isPalindrome("notion")); } // Returns true if the given string reads the same forwards as backwards. public static boolean isPalindrome(String s) { if (s.length() <= 1) { // base case: one letter or empty string return true; } else { // recursive case: 2 or more letters char first = s.charAt(0); char last = s.charAt(s.length() - 1); String middle = s.substring(1, s.length() - 1); return first == last && isPalindrome(middle); } } }