• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

Java Palindrome Help

19
Posts
12
Years
    • Seen Oct 25, 2012
    Basically, I want a method to check if a given string sequence is a palindrome or not, however disregarding the existence of whiespaces, commas, and apostrophes. Also, we cannot find the reverse of a string then compare it to the original; the method we are using is checking/comparing the first and last character then incrementing/decrementing and again comparing.

    The method I came up with is:

    Code:
    public static boolean isPalindromePhrase(String str)
        {
            if (str.length() <= 1) // a one-character string is always a palindrome
                return true;
                
            char rightChars, leftChars;
            int first = 0;
            int last = str.length() - 1;
            
            while (first < last)
            {
                leftChars = str.charAt(first); // characters from the left (moving from left to right)
                leftChars = Character.toLowerCase(leftChars); 
                rightChars = str.charAt(last); // characters from the left (moving from right to left)
                rightChars = Character.toLowerCase(rightChars);
                
                if (leftChars == ' ' || leftChars == '\'' || leftChars == ',')
                {
                    leftChars = str.charAt(first++);
                }
                if (rightChars == ' ' || rightChars == '\'' || rightChars == ',')
                {
                    rightChars = str.charAt(last--);
                }
                
                if (leftChars == rightChars)
                {
                    first++;
                    last--;
                }
                else
                    return false;
            }
            return true;
        }
    Logically, this seems okay, but a string such as "Madam, I'm Adam" is not working. Technically, it is not a true palindrome, but again I was the program to disregard spaces, commas, and apostrophes. The assignment is due for tomorrow, and this is the only problem I have yet. I tried asking programming forums, but they all were misleading/complicated to fathom considering I am still an amateur in java.

    Any help is appreciated, if any.

    *please move to its suitable subforum if not right here.
     
    70
    Posts
    13
    Years
    • Seen Jun 11, 2023
    Why don't you just make two methods? One that removes all the crap you don't need and one that checks for the palindrome?
     
    3,956
    Posts
    17
    Years
  • At a quick glance, you're only doing a single test on whether the given side has an illegal character and incrementing/decrementing respectively. So if you have two ore more adjacent characters, such as a comma and a space, then you are only checking for the first one. Shouldn't they be while loops, instead?

    But SpeedyGeek is correct. If you can create a function to trim the string down and another to check palendomicy, that would be much neater. Even if you just nest one inside the other so the variable stays as-is outside of the scope of these two functions.
     
    Back
    Top