xxxxxxxxxx
// Given a string, return true or false depending if the string
// is a palindrome. Palindromes are words that read the same backwards
// and forwards. Make sure it is case insensitive!
// --- Examples:
// palindrome("Madam") === true
// palindrome("love") === false
function palindrome(str) {
return (str.toLowerCase().split("").reverse().join("") === str.toLowerCase())
}
xxxxxxxxxx
#include <stdio.h>
#include <stdbool.h>
bool is_palindrome(int n ){
int rev = 0, orig = n;
while( n != 0){
rev = 10*rev + (n % 10);
n = n/10;
}
return orig == rev;
}
int main() {
printf("Enter a number");
int n = 0;
if( scanf("%d",&n)!=1 ){
prinf("Bad input\n");
}else{
if(is_palindrome(n))
printf("%d is a palindrome", n);
else
prinf("%d is not a palindrome", n);
}
return 0;
}
xxxxxxxxxx
function palindrome(str) {
var re = /[\W_]/g;// representing Non-alphanumetic characters
var lowRegStr = str.toLowerCase().replace(re, '');
var reverseStr = lowRegStr.split('').reverse().join('');
return reverseStr === lowRegStr;
}
palindrome("A man, a plan, a canal. Panama");
xxxxxxxxxx
function isPalindrome(sometext) {
var replace = /[.,'!?\- \"]/g; //regex for what chars to ignore when determining if palindrome
var text = sometext.replace(replace, '').toUpperCase(); //remove toUpperCase() for case-sensitive
for (var i = 0; i < Math.floor(text.length/2) - 1; i++) {
if(text.charAt(i) == text.charAt(text.length - 1 - i)) {
continue;
} else {
return false;
}
}
return true;
}
//EDIT: found this on https://medium.com/@jeanpan/javascript-splice-slice-split-745b1c1c05d2
//, it is much more elegant:
function isPalindrome(str) {
return str === str.split('').reverse().join('');
}
//you can still add the regex and toUpperCase() if you don't want case sensitive
xxxxxxxxxx
bool isPalindrom(string str){
for (int i = 0; i < str.length(); i++)
if(str[i]!=str[str.length()-i]) return false;
return true;
}
xxxxxxxxxx
#palindrome program in python
n=int(input('Enter a number :'))
num=n
rev=0
while n>0:
r=n%10
rev=rev*10+r
n//=10
print('Reverse of',num,'=',rev)
if rev==num:
print(num,'is a palindrome number')
else :
print(num,'is not a palindrome number')
#output
Enter a number :132
Reverse of 132 = 231
132 is not a palindrome numbe
________________________________________________________________________________
Enter a number :451
Reverse of 451 = 154
451 is not a palindrome number
________________________________________________________________________________
Enter a number :12321
Reverse of 12321 = 12321
12321 is a palindrome number
xxxxxxxxxx
def is_palindrome(s):
string = s
if (string==string[::-1]):
print("The string IS a palindrome")
else:
print("The string is NOT a palindrome")
return
xxxxxxxxxx
var letters = [];
var word = "racecar" //bob
var rword = "";
//put letters of word into stack
for (var i = 0; i < word.length; i++) {
letters.push(word[i]);
}
//pop off the stack in reverse order
for (var i = 0; i < word.length; i++) {
rword += letters.pop();
}
if (rword === word) {
console.log("The word is a palindrome");
}
else {
console.log("The word is not a palindrome");
}
xxxxxxxxxx
# Program to check if a string is palindrome or not
my_str = 'aIbohPhoBiA'
# make it suitable for caseless comparison
my_str = my_str.casefold()
# reverse the string
rev_str = reversed(my_str)
# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
xxxxxxxxxx
var isPalindrome = function(x) {
let y = x
let r = y.toString().split('').reverse().join('')
let t = Number(r)
if(t ===x){
return true
}
else{
return false}
}