0

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.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
FredTheHomie
  • 31
  • 2
  • 8
  • Can you explain what your strategy is here – Tyler Apr 10 '16 at 19:45
  • You need to debug your program to figure out the "some reason" which is preventing your program from passing the tests you run against it. if you add `System.out.println("'" + inputString + "','" + reverse + "'");` after your `for()` loop finishes, what does it say? – Dan O Apr 10 '16 at 19:48
  • Well I first try to reverse my inputString, I then put that if the inputString == the reverse of the inputString then it will return true. I also have it to ignore case and symbols and numbers. That is my strategy, Tyler. – FredTheHomie Apr 10 '16 at 20:00
  • Dan, this is what I get: – FredTheHomie Apr 10 '16 at 20:00
  • I'd follow @DanO's suggestion, but here's one other thing (it won't fix the problem but may help avoid other problems): Don't keep repeating `inputString.replaceAll("[^a-zA-Z ]", "")`. Assign it to a variable and use that. – ajb Apr 10 '16 at 20:00
  • 'Madam, I'm Adam','madA mI madaM' – FredTheHomie Apr 10 '16 at 20:02
  • 1
    you also need to ignore white spaces – Yev Apr 10 '16 at 20:03
  • OK, I think you need to go one further. Follow my suggestion and assign the result of `replaceAll` to a variable, then follow DanO's suggestion but put that variable in your `println` instead of `inputString`. That will show you exactly which variables you're comparing with `equalsIgnoreCase`. That should make it obvious what the problem is. – ajb Apr 10 '16 at 20:03
  • This is one of the lines it printed, it seems the inputString contains symbols but the reverse doesn't. Could this be the reason? – FredTheHomie Apr 10 '16 at 20:04
  • It would make it all a lot simpler (and more efficient) if you stopped doing the `replaceAll` call all the time: simply assign a new variable `String inputStringStripped = inputString.replaceAll("[^A-Za-z]", "");`, then use `inputStringStripped` instead of `inputString.replaceAll("[^A-Za-z]", "")`. – Andy Turner Apr 10 '16 at 20:09

2 Answers2

1

In order to such algorithm to work you need to consider to clean the punctual symbols like comma, semmicolon, aposthrofs etc (am talking about ';:' etc)

Java has a StringBuilder class and in there is a method to reverse strings

so if you clean the String to check, reverse it and compare it again itself you can see if it a palindrome or not...

Example:

public static void main(String[] args) {
    String word = "Madam, I'm Adam";
    String word2 = "addbda";
    String word3 = "";
    String word4 = "Dammit, I'm mad";
    System.out.println(IsPalindrome(word));
    System.out.println(IsPalindrome(word2));
    System.out.println(IsPalindrome(word3));
    System.out.println(IsPalindrome(word4));
}

private static boolean IsPalindrome(String word) {
    String wordClean = word.replaceAll("[^a-zA-Z  ]", "");
    String reversedWord =  new StringBuilder(wordClean).reverse().toString();
    System.out.println(wordClean);
    System.out.println(reversedWord);
        return reversedWord.equalsIgnoreCase(wordClean)&&!wordClean.isEmpty();
}

this will print

MadamImAdam madAmImadaM true

addbda adbdda false

false (empty check)

DammitImmad dammItimmaD true

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Why not just replace everything that's not in `[A-Za-z]` with nothing, rather than just removing specific characters outside that range? – Andy Turner Apr 10 '16 at 20:07
  • The third one should come out as false, not true. Everything else worked fine. The third one is when the inputString is null or "". What can I do to deal with that? – FredTheHomie Apr 10 '16 at 20:13
  • @Xoce웃Пepeúpa it's basically what OP does in the question using `[^A-Za-z ]` - it's just that includes space too. – Andy Turner Apr 10 '16 at 20:14
  • @FredTheHomie thanks, I forgot to check if string is empty, now in there in the return statement... give a try and let me know... – ΦXocę 웃 Пepeúpa ツ Apr 10 '16 at 20:26
0

You are not removing spaces, so only strings with spaces in the same places are good. Just use [^a-zA-Z] against [^a-zA-Z ]

dey
  • 3,022
  • 1
  • 15
  • 25