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
/*
C++ Palindrome Checker program by lolman_ks.
Checks palindromic string WITHOUT reversing it.
Same logic can be used to build this program in other languages.
*/
/*
Logic: In a palindromic string, the 1st and last, 2nd and 2nd last, and so
on characters are equal.
Return false from the function if any of these matches, i.e (1st and last),
(2nd and 2nd last) are not equal.
*/
#include <iostream>
using namespace std;
bool checkPalindromicString(string text){
int counter_1 = 0; //Place one counter at the start.
int counter_2 = text.length() - 1; //And the other at the end.
//Run a loop till counter_2 is not less that counter_1.
while(counter_2 >= counter_1){
if(text[counter_1] != text[counter_2]) return false; //Check for match.
//Note: Execution of a function is halted as soon as a value is returned.
else{
++counter_1; //Move the first counter one place ahead.
--counter_2; //Move the second character one place back.
}
}
return true;
/*
Return true if the loop is not broken because of unequal characters and it
runs till the end.
If the loop runs till the condition specified in it, i.e
(counter_2 >= counter1), that means all matches are equal and the string is
palindromic.
*/
}
//Implementation of the function.
int main(){
cout << checkPalindromicString("racecar") << endl; //Outputs 1 (true).
cout << checkPalindromicString("text") << endl; //Outputs 0 (False).
if(checkPalindromicString("lol")){
cout << "#lolman_ks";
}
//Outputs #lolman_ks.
return 0;
}
//I hope this would be useful to you all, please promote this answer if it is.
//#lolman_ks
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
#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
// The Setup
function palindrome(str) {
// Using Regex to remove all the special character, converted all the string to lowercase to ease working with and assign it to a new variable
let newStr = str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
// Split the string, reverse, join then assign it to a new variable
let revStr = newStr.split('').reverse().join('');
// return their value
return newStr === revStr;
}
palindrome("A man, a plan, a canal. Panama");
xxxxxxxxxx
const palindrome = str = {
str = str.replace(/[\W+|_]/g, '').toLowerCase()
const str1 = str.split('').reverse().join('')
return str1 === str
}
palindrome("My age is 0, 0 si ega ym.");
// With love @kouqhar
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");
}