Java Reference
In-Depth Information
Testing a String for Palindrome
If you are an experienced programmer, you may skip this section. This is meant to serve as a simple exercise for beginners.
A palindrome is a word, a verse, a sentence, or a number that reads the same in forward and backward directions.
For example, “Able was I ere I saw Elba” and 1991 are examples of palindromes. Let's write a method that will accept a
string as an argument and test if that string is a palindrome. The method will return true if the string is a palindrome.
Otherwise, it will return false . You will use some methods of the String class that you learned in the previous
sections. The following is the description of the steps to be performed inside the method.
Assume that the number of characters in the input string is n . You need to compare the character at indexes 0 and
(n-1), 1 and (n -2), 2 and (n - 3), and so on. Note that if you continue the comparison, in the end, you will compare
the character at the index (n-1) with the character at index 0, which you have already compared in the beginning.
You need to compare the characters only halfway through. If all comparisons for equality returns true , the string is a
palindrome.
The number of characters in a string may be odd or even. Comparing characters only halfway works in both
cases. The middle of a string varies depending on whether the length of the string is odd or even. For example, the
middle of the string "FIRST" is the character R . What is the middle character in the string "SECOND" ? You can say
there is no middle character in it as its length is even. For this purpose, it is interesting to note that if the number of
characters in the string is odd, you do not need to compare the middle character with any other character.
You need to continue the character comparison up to the half of the string length if the number of characters
in the string is even, and up to half of the string length minus one if the number of characters is odd. You can get the
numbers of comparisons to be done in both the cases by dividing the length of the string by 2. Note that the length of a
string is an integer and if you divide an integer by 2, the integer division will discard the fraction part, if any, which will
take care of cases with an odd number of characters. Listing 11-4 has the complete code.
Listing 11-4. Testing a String for a Palindrome
// Palindrome.java
package com.jdojo.string;
public class Palindrome {
public static void main(String[] args) {
String str1 = "hello";
boolean b1 = Palindrome.isPalindrome(str1);
System.out.println(str1 + " is a palindrome: " + b1 );
String str2 = "noon";
boolean b2 = Palindrome.isPalindrome(str2);
System.out.println(str2 + " is a palindrome: " + b2 );
}
public static boolean isPalindrome(String inputString) {
// Check for null argument.
if (inputString == null) {
throw new IllegalArgumentException("String cannot be null.");
}
// Get the length of string
int len = inputString.length();
// In case of an empty string and one character strings,
// we do not need to do any comparisions.
// They are always palindromes.
 
Search WWH ::




Custom Search