'How to make palindrome program using recursion ignore special characters in all places of the string in java?

I am having a slight logic error with my palindrome program in that when I insert special characters in the front or end of the string I get an indication that the string is not a palindrome. I am programming the palindrome so that all special characters are being ignored when the string is being considered. For example, @bob would be considered not a palindrome while b@ob would be considered one. How would I go about editing my code to make the special characters be ignored regardless of where the position is located? All of this is being done through recursion.

'''

import java.util.Scanner;

public class recursionExercise {

//the recursive function that checks to see whether the 
//string is a palindrone or not
public static boolean checkPalindrome(String str, int firstChar, int lastChar) {
    //if only one character exists
    if (firstChar == lastChar) 
        return true;
    
    //checks to see if the first and last characters match
    if ((str.charAt(firstChar)) != (str.charAt(lastChar))) 
        
        return false;
    
    //checks to see if it has characters
    if (!isChar(str))
        return true;
    
    
    //checks the middle strings with multiple characters
    //on whether or not they are a palindrome with recursive method
    if (firstChar < lastChar + 1)
        return checkPalindrome(str, firstChar + 1, lastChar - 1 );
                return true;
    
}
   //method that actually determines what a palindrome is
public static boolean isAPalindrome(String str) {
    
    int n = str.length();
    //if string is not 0 or 1 then it's a palindrome
    if(n == 0 || n == 1) 
        return false;
    return checkPalindrome(str, 0, n - 1);
    
}

//method that checks for characters
public static boolean isChar(String str) {
    
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if (!Character.isLetter(c) && !Character.isDigit(c))
            return false;
    }
    return true;
}

//tests out recursive methods
public static void main(String args[]) {
    
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter a string to see if it's a palindrome:");
    
    String str = scanner.nextLine(); //input from the user
    
    
    //checks to see if it's a palindrome and puts them all
    //to be lower case to ignore the case issue
    if(isAPalindrome(str.toLowerCase()))
        System.out.println(str+" is a palindrome");
    
    else
        System.out.println(str+" is not a palindrome");
    
    scanner.close();
}

}

'''



Solution 1:[1]

This one was a doozie for me!! I was just on this problem for school right now, and I was searching for some help online. No luck there!! I didn't have any issue with checking if 1 word was a palindrome, but when came to sentences, I was struggling. SO I had to figure out a way to get rid of the spaces in a string, but while maintaining the integrity of the original string. I tried this code SO many ways, but this was the only way that it worked for me, and passed all of the e-book's tests. Hope this helps!!!

import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        String userText;
        String backText = "";
        String userTextNoSpace = "";
        int i;

        userText = scnr.nextLine();

        if (userText.contains(" ")) {
            userTextNoSpace = userText.replace(" ", "");
            for (i = userTextNoSpace.length() - 1; i >= 0; --i) {
                backText += userTextNoSpace.charAt(i);
            }
        } else {
            for (i = userText.length() - 1; i >= 0; --i) {
                backText += userText.charAt(i);
            }
        }
        if (backText.equalsIgnoreCase(userTextNoSpace) || backText.equalsIgnoreCase(userText)) {
            System.out.println( userText + " is a palindrome");
        } else {
            System.out.println(userText + " is not a palindrome");
        }

    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SweetPea328