what is a palindrome code example
Example 1: palindrome
function isPalindrome(sometext) {
var replace = /[.,'!?\- \"]/g;
var text = sometext.replace(replace, '').toUpperCase();
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;
}
function isPalindrome(str) {
return str === str.split('').reverse().join('');
}
Example 2: palindrome
#include <stdio.h>
int main() {
int n, rev = 0, remainder, num;
printf("Enter an integer: ");
scanf("%d", &n);
num = n;
for(num = n ; n!=0 ; n/=10)
{
remainder = n%10;
rev = rev*10 + remainder;
}
( (rev == num) ? printf("%d is a palindrome.", num) : printf("%d is not a palindrome.", num) );
return 0;
}
Example 3: palindrome
#include<iostream>
#include<string>
#include<algorithm>
bool IsPalindrome_true_false(const std::string& );
int main ()
{
std::cout<<"Please enter a string:\t";
std::string str;
getline(std::cin, str);
int i = 0;
while(str[i])
{
if(str[i] == std::toupper(str[i]) && std::isalpha(str[i]) == 1024)
str[i]+= 32;
++i;
}
while(str.empty())
{
std::cout<<"\nPlease enter a string your string is empty:\t";
if(!str.empty())
std::string str;
getline(std::cin, str);
}
std::cout<<"\n"<<std::boolalpha<<IsPalindrome_true_false(str)<<std::endl;
std::cout<<std::endl;
return 0;
}
bool IsPalindrome_true_false(const std::string& str)
{
int i = 0;
int j = str.length() - 1;
while(i <= j )
{
if(std::isalpha(str[i]) == 0){
++i;
continue;
}else if(std::isalpha(str[j]) == 0){
--j;
continue;
}
if(str[i] != str[j]){
return false;
}
++i;
--j;
}
return true;
}
Example 4: 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;
}
Example 5: palindrome
function isPalindrome(text) {
return [...text].reverse().join('') === text;
}
isPalindrome = text => {
return [...text].reverse().join('') === text;
}
isPalindrome = text => [...text].reverse().join('') === text;
Example 6: palindrome
#include <stdio.h>
int main() {
int n, reversedN = 0, remainder, originalN;
printf("Enter an integer: ");
scanf("%d", &n);
originalN = n;
while (n != 0) {
remainder = n % 10;
reversedN = reversedN * 10 + remainder;
n /= 10;
}
if (originalN == reversedN)
printf("%d is a palindrome.", originalN);
else
printf("%d is not a palindrome.", originalN);
return 0;
}