public class Palindrome {
public static void main(String[] args) {
String str = "racecar";
if (isPalindrome(str)) {
System.out.println(str + " is a palindrome.");
} else {
System.out.println(str + " is not a palindrome.");
}
}
public static boolean isPalindrome(String str) {
if (str.length() <= 1) {
return true;
} else {
char first = str.charAt(0);
char last = str.charAt(str.length() - 1);
if (first == last) {
return isPalindrome(str.substring(1, str.length() - 1));
} else {
return false;
}
}
}
}
racecar is a palindrome.str and initialize it with the value "racecar". We then call the isPalindrome method, passing in str, and print the appropriate message depending on whether the method returns true or false.isPalindrome method is defined as a static method that takes a string parameter str and returns a boolean. The method uses recursion to check if the string is a palindrome.