A palindrome is any data sequence that reads exactly the same forwards as it does backwards.
To test a sequence to see if it is a palindrome or not, we begin by pointing to the elements at the beginning and end of the sequence. So long as the pointers do not meet or cross each other and both refer to the same value, we advance the pointers one element towards the middle of the sequence. If the pointers meet or cross each other, the sequence is a palindrome, otherwise it is not.
The following tests a vector of some type T:
template<typename T>
bool is_palindrome (const std::vector<T>& v) {
size_t x {0}, y {v.size()-1);
while (x<y && v[x]==v[y]) ++x, --y;
return x<=y;
}
Example usages:
std::vector<double> d {1.2, 3.4, 5.6, 3.4, 1.2}; // is a palindrome std::vector<int> i {1, 2, 3, 4, 5}; // is not a palindrome
std::vector<char> c {'m', 'a', 'd', 'a', 'm'}; // is a palindrome
assert (is_palindrome (d) == true);
assert (is_palindrome (i) == false);
assert (is_palindrome (c) == true);
When testing strings, we typically strip out all non-alpha-numeric characters and use a case-insensitive comparison. To achieve this, we simply convert the string to a lower-case character array and invoke the previous function:
bool is_palindrome (const std::string& str) {
std::vector<char> v {};
for (size_t i=0; i<str.size(); ++i)
if (str[i]>='0' && str[i]<='9') v.push_back (str[i]); // push digit characters
else if (str[i]>='a' && str[i]<='z') v.push_back (str[i]); // push lowercase characters
else if (str[i]>='A' && str[i]<='Z') v.push_back (str[i]+32); // push uppercase as lowercase characters
return is_palindrome (v);
}
Example usage:
std::string s {"Madam, I'm Adam!");
assert (is_palindrome (s) == true);
// returns true if num is a palindrome (e.g., 12321 is a palindrome)bool is_palindrome (unsigned num) {
unsigned rev = 0; // initialise the reverse of the number
while (num) { // repeat until num is zero
rev *= 10; // move all digits in the reverse number one position to the left
rev += num%10; // add the least significant digit of the number
num /= 10; // strip off the least-significant digit
}
return num == rev; // test for equality
}
You don't have to write palindrome programs in wikipedia.
stats
#include <stdio.h> #include <conio.h> void main() { int num,rev=0,m,r; clrscr(); printf("enter any number"); scanf("%d",&num); m=num; while(num>0) { r=num%10; rev=rev*10+r; num=num/10; } if(rev==m) printf("given number is palindrome"); else printf("given number is not palindrome"); getch(); } this is the answer.
#include<stdio.h> #include<conio.h> #include<string.h> void main() { int first,last,flag=1; char str[100]; clrscr(); printf("Enter number to get to check wheather palindrome or not"); gets(str); first=0; last=strlen(str)-1; printf("%d",first); printf("%d",last); while(first<=last) { if(str[first]!=str[last]) flag=0; first++; last--; } if(flag==1) { clrscr(); printf("this is palindrome"); getch(); } else { clrscr(); printf("sorry this is not a palindrome"); getch(); } }
No. To be a palindrome the number must read the same forwards as backwards. "153" is not the same number as "351" so this is not palindromic!
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[15],b[15]; printf("Enter the string\n"); scanf("%s",&a); strcpy(b,a); strrev(a); if(strcmp(a,b)==0) printf("The String is a palindrome"); else printf("The String is not a palindrome"); }
"Aha" is a palindrome that can be used as a preposition.
No, it isn't a palindrome.
The palindrome is Level.
There is no palindrome for 14.
Any number that is is a palindrome will always be a palindrome.
No. A palindrome reads the same backwards and forwards. 5791111975 is an example of a palindrome.
1998 is not and cannot be a palindrome.
"radar" is a palindrome for detector.
Radar is a palindrome for detector.
606 is a palindrome.
The palindrome is Noon.