Reputation: 79
I want to put a ignore Upper/Lower case, punctuation, and spaces. Here is the code I want to invoke it into:
public static boolean isPal(String s)
{
if(s.length() == 0 || s.length() == 1)
return true;
if(s.charAt(0) == s.charAt(s.length()-1))
return isPal(s.substring(1, s.length()-1));
return false;
}
Upvotes: 1
Views: 263
Reputation: 18450
Easiest way is to convert your string to lower or upper case and do all the comparisons, e.g.
public static boolean isPal(String s)
{
s = s.toLowerCase().replaceAll("[^a-z]", "");
// Or if you want to allow digits
//s = s.toLowerCase().replaceAll("[^\\w]", "");
// Do all your comparisons
}
Upvotes: 1
Reputation: 13707
You can convert the string to either case and use it for processing -
public static boolean isPal(String s) {
s = s.toLowerCase(); // or s.toUpperCase()
if(s.length() == 0 || s.length() == 1)
return true;
if(s.charAt(0) == s.charAt(s.length()-1))
return isPal(s.substring(1, s.length()-1));
return false;
}
And to ignore the punctuation use can use the regex given below. Add more characters that you need to ignore and escape them using a backslash \
[?:\\/\.]
Add it to the replaceAll()
method to replace with nothing.
Upvotes: 1
Reputation: 129537
You could try adding the line:
s = s.toLowerCase().replaceAll("[\\p{Punct}\\s]", "");
at the start of the method.
Upvotes: 5