newbie
newbie

Reputation: 1980

Java: Remove non alphabet character from a String without regex

Is there a way to remove all non alphabet character from a String without regex? I'm trying to check if the String is a palindrome

This is what i tried so far.

    public static boolean isPalindrome( String text )
    {
        int textLength = text.length() - 1;
        String reformattedText = text.trim().toLowerCase();
        for( int i = 0; i <= textLength; i++ )
        {
            if( reformattedText.charAt( i ) != reformattedText.charAt( textLength - i ) )
            {
                return false;
            }
        }

        return true;
    }

But if the input is:

System.out.println( isPalindrome( "Are we not pure? No sir! Panama’s moody"
            + "Noriega brags. It is garbage! Irony dooms a man; a prisoner up to new era." ) );

It should be true.

I'm really having a hard time thinking of how to remove or ignore those non alphabet characters on the String.

Upvotes: 1

Views: 2750

Answers (3)

Mike Housky
Mike Housky

Reputation: 4069

OOPS. Java, not Python.

You can still use list-like access in Java, just a bit more work.

char[] letters = text.toCharArray(); 
int nletters = 0;
for (int i=0; i<letters.length; ++i) {
    if (Character.isLetter(letters[i])
        letters[nletters++] = Character.toUpperCase(letters[i]);
}
// print out letters in array:
System.out.print("letters only: ");
for (int i=0; i<nletters; ++i) {
    System.out.print(letters[i]);
}
System.out.println();

Now use the first nletters positions only in the letters array, since those positions will hold the lowercased letters from the input. An example that just displays the remaining characters is included above.

Now write a loop to compare letters[0] with letters[nletters-1], letters[1] with letters[nletters-2], and so on. If all pairs are equal, you have a palindrome.

Upvotes: 1

PFROLIM
PFROLIM

Reputation: 1194

I would do something like this:

public static String justAlphaChars(String text) {

    StringBuilder builder = new StringBuilder();

    for (char ch : text.toCharArray()) 
        if (Character.isAlphabetic(ch)) 
            builder.append(ch);

    return builder.toString();
}

Just tested method above in your example bellow and worked. Returned true.

System.out.println( isPalindrome( justAlphaChars ( "Are we not pure? No sir! Panama’s moody"
        + "Noriega brags. It is garbage! Irony dooms a man; a prisoner up to new era." ) ) );

Upvotes: 1

William Madruga
William Madruga

Reputation: 31

String removeNonAlpha(final String word) {
    final StringBuilder result = new StringBuilder();

    for (final char ch : word.toCharArray()) {
        final int ascii = ch;
        if (((ascii >= 65) && (ascii <= 90)) || ((ascii >= 97) && (ascii <= 122))) {
            result.append(ch);
        }
    }
    return result.toString();
}

Explanation: The method will retrieve a string containing only A-Z and a-z characters. I am simply verifying the ascii code for the given char. Please refer to the ASCII code table

Upvotes: 0

Related Questions