Please write the following answer in JAVA (not javascript). Please provide answe
ID: 3861755 • Letter: P
Question
Please write the following answer in JAVA (not javascript). Please provide answer in copyable text. Show input and output. Thanks in advance.
1) A palindrome is a word or phrase that reads the same forward and backwards. A simple example of a palindrome is the word "radar". "Toyota" is not a palindrome but the phrase "A Toyota" is a palindrome. Write a program that calls an isPalindrome method that returns true or false if the phrase is or is not a palindrome. Your palindromeTester() program should call isPalindrome for each of the test strings listed below. After each line of input, print whether the input string is a palindrome or not. Your is Palindrome() method should ignore case, punctuation, and white space. Your program should have this structure: main() palindrome Tester called from main for each test string boolean isPalindrome (String s) called from palindromeTester You must test your program with all of these strings: "To be or not to be" "A Santa as NASA" Acrobats stab orca" "Senile felines" "A man, a plan, a canal: Panama" Madam, I'm Adama" "Doc, note: I dissent. A fast never prevents a fatness. I diet on cod" "radar" Your test output should look like this: "To be or not to be" is NOT palindrome "A Santa as NASA" is NOT a palindrome Acrobats stab orca is a palindrome etc. DON'T PRINT IN THE isPalindrome() method, use .toString and print in the main methodExplanation / Answer
Code:-
//Class to check all the given Strings whether they are palindromes or not.
public class CheckPalindromes {
public static String[] testData = {"To be or not to be","A santa as NASA","Acrobats stab orca","senile felines","A man,A Plan, A canal: panama","Madam, I'm adama","Doc, note: I dissent. A fast never prevents a fatness.I diet on cod","radar"};
public static boolean isPalindrome(String str)
{
//Remove all the Punctuaton marks, White spaces and convert to lower case.
String result = str.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
// Start from leftmost and rightmost corners of result
int l = 0;
int h = result.length() - 1;
// Keep comparing characters while they are same
while (h > l)
{
if (result.charAt(l++) != result.charAt(h--))
{
return false;
}
}
return true;
}
//palindromeTester function takes all the input strings and check whether they are palindromes or not.
public static void palindromeTester(){
for (String str : testData){
if(isPalindrome(str)){
System.out.println(str+" is a Palindrome");
}else{
System.out.println(str+" is NOT a Palindrome");
}
}
}
public static void main(String args[]){
//Cal the function to test the input.
palindromeTester();
}
}
Output:-
To be or not to be is NOT a Palindrome
A santa as NASA is NOT a Palindrome
Acrobats stab orca is a Palindrome
senile felines is a Palindrome
A man,A Plan, A canal: panama is a Palindrome
Madam, I'm adama is NOT a Palindrome
Doc, note: I dissent. A fast never prevents a fatness.I diet on cod is a Palindrome
radar is a Palindrome