Ignore Case and Non-Letters in Palindrome
In the previous step, our palindrome checker worked for simple strings like "madam" and "racecar". However, real-world palindromes often ignore capitalization and punctuation. For example, "A man, a plan, a canal: Panama" is considered a palindrome.
In this step, we will enhance our PalindromeChecker
to handle these cases by ignoring non-letter characters and treating uppercase and lowercase letters the same.
Open the PalindromeChecker.java
file in the WebIDE editor. We will modify the checkPalindrome
method.
Replace the existing checkPalindrome
method with the following updated version:
// Method to check if a string is a palindrome using two pointers,
// ignoring case and non-letter characters
public static boolean checkPalindrome(String str) {
// Initialize two pointers
int left = 0; // Pointer starting from the beginning
int right = str.length() - 1; // Pointer starting from the end
// Loop while the left pointer is less than the right pointer
while (left < right) {
// Move left pointer past non-letter characters
while (left < right && !Character.isLetter(str.charAt(left))) {
left++;
}
// Move right pointer past non-letter characters
while (left < right && !Character.isLetter(str.charAt(right))) {
right--;
}
// If left and right pointers haven't crossed and characters don't match (ignoring case)
if (left < right && Character.toLowerCase(str.charAt(left)) != Character.toLowerCase(str.charAt(right))) {
// If characters don't match, it's not a palindrome
return false;
}
// Move the pointers inwards
left++;
right--;
}
// If the loop completes without finding any mismatch, it's a palindrome
return true;
}
Here are the changes we made:
- We added two
while
loops inside the main while (left < right)
loop.
- The first inner
while
loop (while (left < right && !Character.isLetter(str.charAt(left)))
) moves the left
pointer forward as long as it's still to the left of the right
pointer and the character at the left
pointer is not a letter (!Character.isLetter(...)
).
- The second inner
while
loop (while (left < right && !Character.isLetter(str.charAt(right)))
) moves the right
pointer backward as long as it's still to the left of the right
pointer and the character at the right
pointer is not a letter.
- The comparison
if (left < right && Character.toLowerCase(str.charAt(left)) != Character.toLowerCase(str.charAt(right)))
now includes two important checks:
left < right
: This ensures we only compare characters if the pointers haven't crossed after skipping non-letters.
Character.toLowerCase(...)
: This converts both characters to lowercase before comparing them, effectively ignoring case.
- If the characters (after converting to lowercase) don't match, we return
false
.
- If they match, we move both pointers inwards (
left++
, right--
) to check the next pair of characters.
Now, let's modify the main
method to test with a string that includes spaces, punctuation, and mixed case. Replace the existing test cases in the main
method with this:
public static void main(String[] args) {
String testString = "A man, a plan, a canal: Panama"; // A classic palindrome
boolean isPalindrome = checkPalindrome(testString);
if (isPalindrome) {
System.out.println("\"" + testString + "\" is a palindrome.");
} else {
System.out.println("\"" + testString + "\" is not a palindrome.");
}
testString = "No 'x' in Nixon"; // Another palindrome
isPalindrome = checkPalindrome(testString);
if (isPalindrome) {
System.out.println("\"" + testString + "\" is a palindrome.");
} else {
System.out.println("\"" + testString + "\" is not a palindrome.");
}
testString = "hello world"; // Not a palindrome
isPalindrome = checkPalindrome(testString);
if (isPalindrome) {
System.out.println("\"" + testString + "\" is a palindrome.");
} else {
System.out.println("\"" + testString + "\" is not a palindrome.");
}
}
Save the PalindromeChecker.java
file.
Compile and run the updated program in the Terminal:
javac PalindromeChecker.java
java PalindromeChecker
You should now see the following output, correctly identifying the palindromes even with non-letter characters and mixed case:
"A man, a plan, a canal: Panama" is a palindrome.
"No 'x' in Nixon" is a palindrome.
"hello world" is not a palindrome.
You have successfully enhanced your palindrome checker to handle more complex strings!