Search code examples
javapalindrome

How do you ignore white spaces and punctuation?


How do you make this return the palindrome boolean value ignoring spaces and punctuation?

import java.util.Scanner;
public class StringUtil
{
    public static boolean Palindrome(String s)
    {
        if(s.length() == 0 || s.length() == 1)
            return true;

        if(s.charAt(0) == s.charAt(s.length()-1))
            return Palindrome(s.substring(1, s.length()-1));

        return false;
    }

    public static void main(String[]args)
    {
        Scanner check = new Scanner(System.in);
        System.out.println("type in a string to check if its a palindrome or not");
        String p = check.nextLine();
        if(Palindrome(p))
            System.out.println(p + " is a palindrome");
        else
            System.out.println(p+ " is not a palindrome");
    }
}

Solution

  • Check out String.replaceAll, you define what you want to replace, in this case, whitespaces and punctuation, so we will use \\W as what we want to find and replace it with nothing.

    import java.util.Scanner;
    public class StringUtil{
    
    public static boolean Palindrome(String s)
    {
    
        if(s.length() == 0 || s.length() == 1)
            return true;
    
        if(s.charAt(0) == s.charAt(s.length()-1))
            return Palindrome(s.substring(1, s.length()-1));
    
        return false;
    
    }
    
    public static void main(String[]args)
    {
        Scanner check = new Scanner(System.in);
        System.out.println("type in a string to check if its a palindrome or not");
        String p = check.nextLine();
    
        //We replace all of the whitespace and punctuation 
        p = p.replaceAll("\\W", "");
    
        if(Palindrome(p))
            System.out.println(p + " is a palindrome");
        else
            System.out.println(p+ " is not a palindrome");
    }
    
    }
    

    Sample Output

    type in a string to check if its a palindrome or not
    r';:.,?!ace    car
    racecar is a palindrome