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
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
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}
}
xxxxxxxxxx
# Note: A palindrome is a string that reads the same forwards and backwards.
def palindrome_function(original_list, user_string):
# reversing the list
reversed_list = original_list[::-1]
# comparing reversed_list to original_list to
# determine if the original_list is a palindrome
if reversed_list == original_list:
print("'" + user_string.lower() + "' is a palindrome.")
else:
print("'" + user_string.lower() + "' is not a palindrome!")
def main():
# promting user to input a word or phrase
user_string = input("Enter string: ")
user_list = list(user_string.lower())
# putting our palindrome_function() to use
palindrome_function(user_list, user_string)
if __name__ == "__main__":
main()