Checks whether a given string qualifies as a palindrome or not - Java java.lang

Java examples for java.lang:String Algorithm

Description

Checks whether a given string qualifies as a palindrome or not

Demo Code


//package com.java2s;

public class Main {
    /**/*from  w ww  .  j  av a2s. com*/
     * Checks whether a given string qualifies as a palindrome or not
     * 
     * @param palindromeCandidate
     * @return true is given string is a palindrome
     */
    public static boolean isPalindrome(String palindromeCandidate) {
        boolean evenSized = (palindromeCandidate.length() % 2 == 0);
        int index1 = 0;
        int index2 = palindromeCandidate.length() - 1;
        while ((index1 < index2 && evenSized)
                || (index1 <= index2 && !evenSized)) {
            if (palindromeCandidate.charAt(index1) != palindromeCandidate
                    .charAt(index2)) {
                break;
            } else if ((index1 + 1 == index2 && evenSized)
                    || (index1 == index2 && !evenSized)) {
                return true;
            }
            index1++;
            index2--;
        }
        return false;
    }
}

Related Tutorials