Im trying to make a program that checks for Palindromes

Hello everyone, I just recently got into processing and have a question about how to write a palindrome checker for my analyzer class. Here is the code I have so far but always says palindromes are not palindromes.

Some notes, wordList is the input in the main class using processing 3. The method I am trying to use is comparing the reversed version of the string with itself to see if they are the same. Hope you can help, thanks!

Boolean isPalindrome(String wordList) { String a; a=""; for(int i=0; i<wordList.length();i++) { a = a + wordList.charAt(wordList.length()-1-i); } if(a == wordList) { println(); print("Its a palindrome"); fill(0); textSize(14); text("Its a palindrome",80,170); return true; } else { println(); print("Its not a palindrome"); fill(0); textSize(14); text("Its not a palindrome",80,170); return false; }

Answers

  • Answer ✓

    You need to check this: https://processing.org/reference/draw_.html

    Add the following to your code:

    void setup() {
      println(isPalindrome("aca"));
    }
    
    void draw() {}  //It can be omitted
    

    Also notice this next can be change:

      for (int i=0; i<wordList.length(); i++)
      {
        a = a + wordList.charAt(wordList.length()-1-i);
      }
    

    With this following version, which is easier to read and debug, two traits that your programs should follow.

      for (int i=wordList.length()-1;i>=0 i--)
      {
        a += wordList.charAt(i);
      }
    

    Finally, and very important in your case, read this reference: https://processing.org/reference/equality.html

    Replace if (a.==wordList) with if (a.equals(wordList)) as this will solve your problem. You need to ask yourself why one way is correct and the other one seems correct but it is not.

    Kf

  • edited April 2018

    The loops are good practice, but there's a built in method for reversing strings

    String reversed = StringBuilder(original).reverse().toString();
    

    The .equals is the problem though.

    Note the use of meaningful variable names, not just 'a'

  • edited April 2018

    This is an interesting class of problem to implement without just using .reverse() and comparing, because you might want to include parameters that allow more relaxed rules.

    For some examples:

    "AVA, STOP" -> "POTS, AVA"

    The comma is in the wrong place -- the reverse is "POTS ,AVA" -- but a relaxed reading of how spacing works around punctuation (or ignoring punctuation) could still identify this as valid.

    "Ava, Stop" -> "Pots, Ava"

    Well, 'A' != 'a', so checking the reverse of the string (even without punctuation) would fail because "Ava Stop" = "potS avA" != "Pots Ava". Same-casing everything could fix this.

    "Ava, Stop!" -> "Pots, Ava!"

    Stripping terminal punctuation would render this valid.

    "A man, a plan, a canal: Panama."

    Stripping all punctuation AND all spacing AND ignores case would render this valid -- a common way of defining palindromes, but not the only way.

    If you look at published lists like:

    ...then you will find a lot of interesting test cases to run against your code and see which ones succeed and fail.

    Of course, sometimes you might not want to match as loosely as possible. At other times you might want to be more strict, so that "racecar" matches, but "Amore, Roma" does not....

  • I fixed it, thanks a lot everyone. It was really helpful and all your comments have made me a better programmer

Sign In or Register to comment.