xxxxxxxxxx
#include <stdio.h>
#include <stdbool.h>
bool is_palindrome(int n ){
int rev = 0, orig = n;
while( n != 0){
rev = 10*rev + (n % 10);
n = n/10;
}
return orig == rev;
}
int main() {
printf("Enter a number");
int n = 0;
if( scanf("%d",&n)!=1 ){
prinf("Bad input\n");
}else{
if(is_palindrome(n))
printf("%d is a palindrome", n);
else
prinf("%d is not a palindrome", n);
}
return 0;
}
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
function isPalindrome(str) {
str = str.toLowerCase();
return str === str.split("").reverse().join("");
}
xxxxxxxxxx
<script>
// function that check str is palindrome or not
function check_palindrome( str )
{
let j = str.length -1;
for( let i = 0 ; i < j/2 ;i++)
{
let x = str[i] ;//forward character
let y = str[j-i];//backward character
if( x != y)
{
// return false if string not match
return false;
}
}
/// return true if string is palindrome
return true;
}
//function that print output is string is palindrome
function is_palindrome( str )
{
// variable that is true if string is palindrome
let ans = check_palindrome(str);
//condition checking ans is true or not
if( ans == true )
{
console.log("passed string is palindrome ");
}
else
{
console.log("passed string not a palindrome");
}
}
// test variable
let test = "racecar";
is_palindrome(test);
</script>
xxxxxxxxxx
class Solution :
def isPalindrome(self, string: str ) :
'''
A function to check if a string is Palindrome or not!
:param string: string to be checked
:return: True if it is palindrome else False
'''
string = string.lower()
s_stripped = ''.join(list( filter ( lambda x : x.isalnum () == True , string)))
return s_stripped == s_stripped[::-1]
if __name__ == '__main__' :
string = 'Was it a car or a cat I saw!!'
print (f'Is "{string}" a palindrome? : {Solution ().isPalindrome (string)}')
string2 = 'A man, a plan,'
print (f'Is "{string2}" a palindrome? : {Solution ().isPalindrome (string2)}')
xxxxxxxxxx
// Given a string, return true or false depending if the string
// is a palindrome. Palindromes are words that read the same backwards
// and forwards. Make sure it is case insensitive!
// --- Examples:
// palindrome("Madam") === true
// palindrome("love") === false
function palindrome(str) {
return (str.toLowerCase().split("").reverse().join("") === str.toLowerCase())
}
xxxxxxxxxx
// can we generate palindrome string by suffling
// the character of the string s
public bool CanBePalindrome(string s)
{
var dict = new Dictionary<char, int>();
for (int j =0; j < s.Length; j++)
{
var item = s[j];
if (dict.ContainsKey(item))
{
dict[item]++;
if (dict[item] == 2)
{
dict.Remove(item);
}
}
else
{
dict.Add(item, 1);
}
}
return dict.Count <= 1;
}
xxxxxxxxxx
#include <iostream>
#include <deque>
bool is_palindrome(const std::string &s){
if(s.size() == 1 ) return true;
std::deque<char> d ;
for(char i : s){
if(std::isalpha(i)){
d.emplace_back( toupper(i) );
}
}
auto front = d.begin();
auto back = d.rbegin();
while( (front != d.end()) || (back != d.rend())){
bool ans = (*front == *back);
if(!ans){
return ans;
}
++front;++back;
}
return true;
}
int main() {
std::string s = "A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!";
std::cout << std::boolalpha << is_palindrome(s) << std::endl;
return 0;
}