I have a issue with my palindrome checker as it is not working properly. It must be able to test these 4 things:
TestPalindrome("Madam, I'm Adam", true);
TestPalindrome("addbda", false );
TestPalindrome("", false);
TestPalindrome("Dammit, I'm mad", true);
This is my code for the palindrome:
public static boolean IsPalindrome( String inputString )
{
String reverse = "";
for(int i = inputString.replaceAll("[^a-zA-Z ]", "").length() -1; i>=0; i--){
reverse = reverse + inputString.replaceAll("[^a-zA-Z ]", "").charAt(i);
}
if(inputString.replaceAll("[^a-zA-Z ]", "").equalsIgnoreCase(reverse.toString()) && !inputString.replaceAll("[^a-zA-Z ]", "").isEmpty()){
return true;
}
if(!inputString.replaceAll("[^a-zA-Z ]", "").equalsIgnoreCase(reverse.toString()) && !inputString.replaceAll("[^a-zA-Z ]", "").isEmpty()){
return false;
}
if(inputString.replaceAll("[^a-zA-Z ]", "").isEmpty()){
return false;
}
else return true;
}
}
This is what my output is:
TestPalindrome failed: expected true got false
TestPalindrome passed!
TestPalindrome passed!
TestPalindrome failed: expected true got false
Can anyone help me fix this so I get all passes in the list. I understand similar questions have been asked but I´m not asking how to do a palindrome checker but how to fix my specific one. I used those other questions to learn but for some reason my one doesn't work.