Palindrome Checker
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.");
Palindrome Checker
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.");
palindrome
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
palindrome number
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
what is palindrome
function Palindrome(str) {
str = str.replace(/ /g,"").toLowerCase();
var compareStr = str.split("").reverse().join("");
if (compareStr === str) {
return true;
}
else {
return false;
}
}
Finding palindrome numbers
//Finding palindrome numbers
// first convert to string (slow)
fn is_palindrome1(num: i32) -> bool {
return num.to_string().chars().rev().collect::<String>() == num.to_string();
}
// variation using zip
fn is_palindrome2(num: i32) -> bool {
let s = num.to_string();
for (i1, i2) in s.chars().zip(s.chars().rev()) {
if i1 != i2 {
return false;
}
}
return true;
}
// divide by 10 (fast)
fn is_palindrome3(num: i32) -> bool {
let mut reversed = 0;
let mut number = num;
while number > 0 {
reversed = reversed * 10 + number % 10;
number = number / 10;
}
return reversed == num || num == number / 10;
}
fn main() {
for i in 700..750 {
if is_palindrome1(i) {
println!("Pal1: {}", i);
}
if is_palindrome2(i) {
println!("Pal2: {}", i);
}
if is_palindrome3(i) {
println!("Pal3: {}", i);
}
}
}
check palindrome
bool isPlaindrome(string s)
{
int i=0;
int j=s.length()-1;
while(i<j)
{
if(s[i]==s[j])
{i++;
j--;}
else break;
}
if (i==j || i>j) return 1;
else return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us