answersLogoWhite

0

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

What else can I help you with?

Related Questions

What is the most efficient way to find the longest palindrome subsequence in Java?

One efficient way to find the longest palindrome subsequence in Java is by using dynamic programming. This involves creating a 2D array to store the lengths of palindrome subsequences for different substrings of the input string. By iterating through the string and filling in the array based on the palindrome properties, you can determine the longest palindrome subsequence.


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?

Ere is a preposition that is a palindrome.


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 a palindrome for a musical engagement?

The palindrome is Gig.


What is a palindrome for plastic?

&quot;Saltcaps&quot; is a palindrome for plastic.


What is the palindrome of 1998?

1998 is not and cannot be a palindrome.


Is 1331 a palindrome?

Yes, 1331 is a palindrome