is palindrome code example
Example 1: Write a function that tests whether a string is a palindrome
def palindrome_check(string):
string = list(string)
tmp = []
#remove any spaces
for x in range(0, len(string)):
if(string[x] != " "):
tmp.append(string[x].lower())
#now reverse the string
array1 = []
i = 0
j = len(tmp)-1
while(i < len(tmp)):
array1.append(tmp[j])
i += 1
j -= 1
#check if array1 is equal to the string
counter = 0
for x in range(0, len(tmp)):
if(tmp[x] == array1[x]):
counter += 1
#if the counter is equal to the length of the string then the word
#is the same
if(counter == len(tmp)):
return True
return False
Example 2: check if palindrome
function isPalindrome(str) {
str = str.toLowerCase();
return str === str.split("").reverse().join("");
}
Example 3: 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
Example 4: palindrome
//made by Kashish Vaid the great.
// Palindrome programme using for loop the easiest prgm
#include <stdio.h>
int main() {
int n, rev = 0, remainder, num;
printf("Enter an integer: ");
scanf("%d", &n);
num = n;
// reversed integer is stored in rev
for(num = n ; n!=0 ; n/=10)
{
remainder = n%10;
rev = rev*10 + remainder;
}
// if else shortcuts
( (rev == num) ? printf("%d is a palindrome.", num) : printf("%d is not a palindrome.", num) );
return 0;
}
//made by Kashish Vaid the great.
Example 5: 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.");
Example 6: 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);
// convert the string from uppercase to lowercase
int i = 0;
while(str[i])
{
if(str[i] == std::toupper(str[i]) && std::isalpha(str[i]) == 1024)
str[i]+= 32;
++i;
}
// looping while string is empty
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;
}
// check if string is a palindrome and return true or false
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;
}