Skip to main content

Palindrome words

palindrome is a word, number, phrase, or other sequences of characters which reads the same backward as forward, such as madam or racecar or the number 10801. Sentence-length palindromes may be written when allowances are made for adjustments to capital letters, punctuation, and word dividers, such as "A man, a plan, a canal, Panama!", "Was it a car or a cat I saw?" or "No 'x' in Nixon". Wikipedia

PHP: check the string and reverse it

function isPalindrome($string){
    $string = strtolower($string);
     $string = str_replace(' ', '', $string);
    $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string);
    return (strrev($string) == $string);
var_dump( isPalindrome('Anna') ); // true

Javascript: using a RegEx and converting string to lower case

function palindrome(str) {
 var re = /[\W_]/g;
 var lowRegStr = str.toLowerCase().replace(re, '');
 var reverseStr = lowRegStr.split('').reverse().join('');
 return reverseStr === lowRegStr;
palindrome("A man, a plan, a canal. Panama");

Python lowercase and remove special characters

def isPalindrome(s):
   # Lower case
   s = s.lower()

   # Remove special characters
   s = ''.join(e for e in s if e.isalnum())

   # Reverse string
   rev = s[::-1]

   # Checking if both string are equal or not
   return (s == rev)

# Test
s = "Malayalam"
ans = isPalindrome(s)
if ans == 1: