Palindrome is a string which is spelled the same way forwards and backwards (Exa
ID: 3799207 • Letter: P
Question
Palindrome is a string which is spelled the same way forwards and backwards (Example: abba). Write a program (using recursion), on the template provided below, that will check if a given string is a palindrome ignoring spaces, punctuation marks, and all characters different than lowercase letters in the string. Test your program with the following strings: "a man, a plan, a canal panama" " - " "radars" import java.util.Scanner; public class PalindromeRecursive {public static void main(String[] args) {System.out.print("Enter a string to check if it is palindrome:: "); Scanner input = new Scanner(System.in); String inputstring = input.nextLine(); String palindromeString = cleanupString(inputString); boolean isPalindrome = checkPalindrome(palindromeString, 0, palindromeString.length()-l); if (isPalindrome) {System.out.println("The given string is a palindrome");} else {System.out.println("The given string is not a palindrome");} input.close();}/* Creating a method for performing string clean operation to get rid of unwanted characters */private static String cleanupString(String s) {String cleanupStr = ""/* * Write a logic that should include only lower case alphabets. * It should not include special characters, numbers and spaces. */return cleanupStr;} private static boolean checkPalindrome(String inputstring, int low, int high) {return false;}//Write a logic that should call checkPalindrome(String inputstring, int low, int high) method recursively.Explanation / Answer
import java.util.Scanner;
public class PalindromeRecursive{
public static void main(String[] args){
System.out.print("Enter a string to check if its a palindrome :: ");
Scanner input = new Scanner(System.in);
String inputString = input.nextLine();
String palindromeString = cleanupString(inputString);
boolean isPalindrome = checkPalindrome(palindromeString, 0, palindromeString.length() - 1);
if(isPalindrome){
System.out.println("The given string is a palindrome");
}
else{
System.out.println("The given string is not a palindrome");
}
input.close();
}
static String cleanupString(String str){
String cleanupStr = "";
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) >= 'a' || str.charAt(i) <= 'z'){
cleanupStr += str.charAt(i);
}
}
return cleanupStr;
}
private static boolean checkPalindrome(String inputString, int low, int high){
if(low == high || low == high - 1){
if(inputString.charAt(low) == inputString.charAt(high)) return true;
else return false;
}
else{
return checkPalindrome(inputString, low + 1, high - 1);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.