Tag Archives: palindrome

String palindrome or palindrome words

A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. In ICSE and ISC examinations, lot of problems had been framed on palindromes. In this post I will discuss string palindrome. Although numbers can be tested for being a string palindrome using the same method, students must understand that strings are inherently are inherently slower. I will discuss integer palindrome exclusively in another post.

Testing for palindrome using StringBuffer

A StringBuffer is mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. In order to test for a string palindrome we will use the reverse() method to reverse the string and then compare it with the original string. A Java program to test for a string palindrome is as follows:

class Palindrome{
    public static boolean isPalindrome1(String str){
        StringBuffer sb = new StringBuffer(str);
        String reverse = (sb.reverse()).toString();
        return str.equalsIgnoreCase((reverse));
    }
    public static void main(String args[]){
        String str="Madam";
        System.out.println(isPalindrome1(str));

    }
}

The above program to test for a palindrome words is quite straightforward. A string buffer object is initialized with the original string provided as the argument using the StringBuffer() constructor. The string buffer contents are reversed using the reverse function defined in the StringBuffer class. The reversed string is then converted into string using toString() function and then compared with the original string using the equalsIgnoreCase() function. The equalsIgnoreCase() is used because we don’t want to make any distinction between uppercase and lowercase characters.

Testing for palindrome using StringBuffer

The Java program to test for a string to be palindrome word using StringBuffer would give correct answer, but it is not a very efficient program since the entire string needs to be reversed before the test for equality. On careful observation one would realize that the entire string need not be reversed. Instead, if we check the first and the last character, second and the second last character and so on, we can conclude that the string is not a palindrome as soon as the first mismatch is encountered. When using this method there we only need to iterate forย half the string only because after the halfway mark repetition will start. A Java program to test for a string to be palindrome using this method is as follows:

class Palindrome{
    public static boolean isPalindrome2(String str){
        int len=str.length();
        int half=len/2;
        boolean palindrome=true;
        for(int i=0; i <= half; i++){
            if(Character.toUpperCase(str.charAt(i))!=Character.toUpperCase(str.charAt(len-i-1))){
                palindrome=false;
                break;
            }
        }
        return palindrome;
    }
    public static void main(String args[]){
        String str="Madam";
        System.out.println(isPalindrome2(str));

    }
}

Testing for palindrome using StringBuffer

A StringBuffer is mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. In order to test for a palindrome we will use the reverse() method to reverse the string and then compare it with the original string. A Java program to test for a string to be palindrome is as follows:

class Palindrome{
    public static boolean isPalindrome1(String str){
        StringBuffer sb = new StringBuffer(str);
        String reverse = (sb.reverse()).toString();
        return str.equalsIgnoreCase((reverse));
    }
    public static void main(String args[]){
        String str="Madam";
        System.out.println(isPalindrome1(str));

    }
}

The above program to test for a palindrome words is quite straightforward. A string buffer object is initialized with the original string provided as the argument using the StringBuffer() constructor. The string buffer contents are reversed using the reverse function defined in the StringBuffer class. The reversed string is then converted into string using toString() function and then compared with the original string using the equalsIgnoreCase() function. The equalsIgnoreCase() is used because we don’t want to make any distinction between uppercase and lowercase characters.

Testing for palindrome using StringBuffer

The first Java program to test for a string to be palindrome word will give correct answer, but is not very efficient since the entire string needs to be reversed before the test for equality. On careful observation one would realize that the entire string need not be reversed. Instead, if we check the first and the last character, second and the second last character and so on, we can conclude that the string is not a palindrome as soon as the first mismatch is encountered. When using this method there we only need to iterate for half the string only because after the halfway mark repetition will start. A Java program to test for a string to be palindrome using this method is as follows:

class Palindrome{
    public static boolean isPalindrome2(String str){
        int len=str.length();
        int half=len/2;
        boolean palindrome=true;
        for(int i=0; i <= half; i++){
            if(Character.toUpperCase(str.charAt(i))!=Character.toUpperCase(str.charAt(len-i-1))){
                palindrome=false;
                break;
            }
        }
        return palindrome;
    }
    public static void main(String args[]){
        String str="Madam";
        System.out.println(isPalindrome2(str));

    }
}

As usual, the main function has purposely been kept to minimum and its expected that students will write appropriate input/output statements as per the requirement.