Home
Map
Palindrome MethodTest for palindromes. Introduce an isPalindrome method and skip non-letter chars while scanning.
Java
This page was last reviewed on Sep 15, 2022.
Palindromes. Often as a programming exercise, we write code to detect palindromes. A palindrome spells the same word (or sentence) forwards and backwards.
By scanning forward from the start, and backwards from the end of a string, we can compare the equality of each character. We can ignore spaces and punctuation as we go along.
This code sample introduces 2 methods. In isPalindrome we tell whether the string argument is a palindrome. It uses a while-true loop (with nested loops) to check characters.
for
Detail The two nested while loops, that test for Character.isLetter, advance past non-letter chars—this skips spaces.
Detail We invoke Character.toLowerCase to convert uppercase letters to lowercase ones. So "X" and "x" are equal.
Detail Here we test an array of Strings for palindromes. We write our results to console.
Array
public class Program { public static boolean isPalindrome(String value) { // Scan towards the middle, adjusting the start and end indexes. int min = 0; int max = value.length() - 1; while (true) { if (min > max) { return true; } // Move forward past non-letter chars at the start. while (!Character.isLetter(value.charAt(min))) { min++; } // Move backward past non-letter chars at the end. while (!Character.isLetter(value.charAt(max))) { max--; } char a = Character.toLowerCase(value.charAt(min)); char b = Character.toLowerCase(value.charAt(max)); // If letters are not equal, this is not a palindrome. if (a != b) { return false; } // Advance towards center. min++; max--; } } public static void main(String[] args) { // Some example lines. String[] lines = { "civic", "A man, a plan, a canal: Panama.", "A Toyota. Race fast, safe car. A Toyota.", "Cigar? Toss it in a can. It is so tragic.", "Dammit, I'm mad!", "Delia saw I was ailed.", "Desserts, I stressed!", "Draw, O coward!", "Lepers repel.", "Live not on evil.", "Lonely Tylenol.", "Murder for a jar of red rum.", "Never odd or even.", "No lemon, no melon.", "Senile felines.", "So many dynamos!", "Step on no pets.", "Was it a car or a cat I saw?", "Dot Net Perls is not a palindrome.", "Why are you reading this?", "This article is not useful." }; // Test each line to see if it is a palindrome. for (String line : lines) { if (isPalindrome(line)) { System.out.println("Palindrome: " + line); } else { System.out.println("Not palindrome: " + line); } } } }
Palindrome: civic Palindrome: A man, a plan, a canal: Panama. Palindrome: A Toyota. Race fast, safe car. A Toyota. Palindrome: Cigar? Toss it in a can. It is so tragic. Palindrome: Dammit, I'm mad! Palindrome: Delia saw I was ailed. Palindrome: Desserts, I stressed! Palindrome: Draw, O coward! Palindrome: Lepers repel. Palindrome: Live not on evil. Palindrome: Lonely Tylenol. Palindrome: Murder for a jar of red rum. Palindrome: Never odd or even. Palindrome: No lemon, no melon. Palindrome: Senile felines. Palindrome: So many dynamos! Palindrome: Step on no pets. Palindrome: Was it a car or a cat I saw? Not palindrome: Dot Net Perls is not a palindrome. Not palindrome: Why are you reading this? Not palindrome: This article is not useful.
Considerations. Often when implementing a palindrome method, non-letter characters are not considered. But when analyzing a sentence, punctuation and spaces usually appear.
So It is possible to preprocess a string, with Replace or a regular expression, to eliminate these characters.
String replace
However For performance, and clarity, it is better to skip past these characters within the isPalindrome method.
When learning a language, I often implement many small methods based on known algorithms. These include a ROT13 cipher and a palindrome-testing method. We can fill in gaps in our knowledge.
ROT13
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 15, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.