Check word for palindrome. - Java java.lang

Java examples for java.lang:String Algorithm

Description

Check word for palindrome.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String word = "java2s.com";
        System.out.println(isPalindrome(word));
    }/*w  w w  .j  a v a 2 s.  c  o m*/

    /**
     * Check word for palindrome.
     * @param word word.
     * @return true - word is palindrome, false - word is not palindrome.
     */
    public static boolean isPalindrome(String word) {
        if (null == word) {
            return false;
        }
        String trimmedString = word.replaceAll(" ", "");

        if (trimmedString.equals("")) {
            return false;
        }

        String reverseTrimmedString = new StringBuilder(trimmedString)
                .reverse().toString();
        return trimmedString.equalsIgnoreCase(reverseTrimmedString);
    }
}

Related Tutorials