Palindrome is a sequence of characters, which has the same meaning when read in either direction (left to right, right to left). When deciding whether the given word (phrase, number) is a palindrome, we usually omit white characters, punctuation marks and diacritics.
Among the basic types of palindromes belong palindromic words (radar, civic, level, rotor...), sentences (“Madam, I'm Adam”, “Never odd or even”...), numbers (99, 191, 112211...) a and dates (01/02/2010, 10/12/2101...). Sometimes whole words are considered as units (“King, are you glad you are king?”).
Code
/** * Check whether the given string is a palindrome * @param tested tested string * @param ignoreLetterCase flag indicating whether the letter case will be ignored - * true: case will be ignored, false: case won't be ignored * @param ignoreWhitespaces flag indicating whether white characters will be ignored. * true: white characters will be ignored, false: white characters won't be ignored * @param ignoreDiacritics flag indicating whether diacritics will be ignored. * true: diacritics will be ignored, false: diacritics won't be ignored * @return true: if the testing string is palindrome, false: otherwise */ public static boolean isPalindrome(String tested, boolean ignoreLetterCase, boolean ignoreWhitespaces, boolean ignoreDiacritics){ if(ignoreLetterCase){ tested = tested.toLowerCase(); } if(ignoreWhitespaces){ tested = tested.replaceAll("\\\\s", ""); //replace white characters with an empty string } if(ignoreDiacritics){ tested = Normalizer.normalize(tested, Form.NFD).replaceAll("\\\\p{InCombiningDiacriticalMarks}+", ""); //decompose the character and remove the accent part } //check whether the string is a palindrome for(int i = 0; i < tested.length()/2; i++){ if(tested.charAt(i) != tested.charAt(tested.length() - 1 - i)){ return false; } } return true; }