answersLogoWhite

0


Best Answer

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

User Avatar

Wiki User

7y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

6y ago

// 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

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is palindrome in programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Program to find palindrome using C programming?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; #include&lt;string.h&gt; void main() { char a[15],b[15]; printf("Enter the string\n"); scanf("%s",&amp;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"); }


Which palindrome is a preposition?

&quot;Aha&quot; is a palindrome that can be used as a preposition.


Is glockenspiel a palindrome?

No, it isn't a palindrome.


What is the palindrome of balanced?

The palindrome is Level.


What is the palindrome of 14?

There is no palindrome for 14.


Which 4 digit number is always a palindrome?

Any number that is is a palindrome will always be a palindrome.


Is 107541 a palindrome?

No. A palindrome reads the same backwards and forwards. 5791111975 is an example of a palindrome.


What is the palindrome of 1998?

1998 is not and cannot be a palindrome.


What is the palindrome for detector?

"radar" is a palindrome for detector.


What is a palindrome for detector?

Radar is a palindrome for detector.


Is 654 palindrome or 606?

606 is a palindrome.


What time word is a palindrome?

The palindrome is Noon.