Answers for "what is palindrome"

1

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.");
Posted by: Guest on February-27-2021
5

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
Posted by: Guest on November-23-2020
1

palindrome number

while(n>0){    
   r=n%10;  //getting remainder  
   sum=(sum*10)+r;    
   n=n/10;    
  }
Posted by: Guest on June-08-2021
0

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;
  } 

}
Posted by: Guest on June-17-2021
0

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);
        }
    }
}
Posted by: Guest on September-06-2021
0

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;
	}
Posted by: Guest on October-23-2020

Browse Popular Code Answers by Language