Question:
How to make a palindrome program that reads an array of characters and determines if it's a palindrome?
Jay
2015-10-28 16:13:35 UTC
I think to determine whether it is palindrome I should first check the first element with the last one, then the second element with the second to last, and so on. But not sure how to go about this at all.

Please help
Thank you
Three answers:
2015-10-28 16:55:25 UTC
I'm a firm believer in, "You must be able to do a portion of the task manually so that you understand what you're doing. Only then can you begin the coding process."



So, how would you **manually** check the following phrase to see if it's a palindrome:



Madam, I'm Adam



How about:

1. Get the user's input. We did. It's: Madam, I'm Adam

2. Consider removing any punctuation and spaces, which leaves: MadamImAdam

3. Convert to lowercase and count the size of the phrase: madamimadam -- 11 chars.



If the size is an even number, split the phrase in half and compare each char. (See example)



If the size is an odd number, subtract 1 then split the phrase in half (this omits the middle char) and compare each char. (Again, see the example)



Example: Madam, I'm Adam

After doing steps 1-3, we have: madamimadam = 11 chars

a. subtract 1, gives us 10. Cut 10 in half, you get 5.

b. Grab the 1st 5 chars from the front and the last 5 chars from the rear.

c. Compare char 1 in string-1 to char 5 in string-2, and so on...

d. If they're equal you have a palindrome.



Some test phrases and the expected result:

------------------------- --------------------------------

Madam, I'm Adam -- (Palindrome)

Madam, I am Adam -- (NOT a Palindrome)



Eva, Can I Stab Bats In A Cave? -- (Palindrome)

Eva, Can I Stab Bats In A Cove? -- (NOT a Palindrome)



A Man, A Plan, A Canal -- Panama! -- (Palindrome)

A Man, A Plann, A Canal -- Panama! -- (NOT a Palindrome)
brilliant_moves
2015-10-28 16:29:44 UTC
What programming language are you using?
Quentin
2015-10-28 19:06:01 UTC
void remove(string& str, string ignore){

auto end = str.end();

for(auto c: ignore) end = remove(str.begin(), end, c);

str.erase(end, str.end());

}



bool palindrome(string str){

remove(str, ",!?' -."); // remove all the spaces etc

int p = str.size(); // get the number of characters remaining

bool pal = true;

for(int i=0; pal && i< p/2; i++){// for half the characters and its still a palindrome so far

pal = toupper(str[i]) == toupper(str[p-1-i]); // keep testing if they matching characters are equal



}

return pal;// return the result

}



/****** ****** ****** ****** ****** ****** ****** ****** ****** ****** ****** ****** ******/

// same as the above but jumping over the characters to be ignored instead of removing them from the string



#include

#include

#include

using namespace std;



template

bool palindrome(string str, MATCH match, IGNORE ignore){

auto pL = str.begin();// start at the left of the string

auto pR = str.end()-1;// start at the right of the string

bool pal = true;



while(pal && *pL && *pR && pL
// jump over characters to be ignored

while(ignore(*pL) && *++pL);

while(ignore(*pR) && *--pR);

// test that haven't jumped past the end

if(*pL && *pR && match(*pL, *pR)){

pL++;

pR--;

}

else {

pal = false;

}

}

return pal;

}



bool characterMatch(char cl, char cr){

return toupper(cl)==toupper(cr);// compare in same case

}



bool characterIgnore(char c){

return !isalpha(c);// anything that's not an alpha is ignored

}



bool palindrome(string str){

return palindrome(str, characterMatch, characterIgnore);

}



USAGE:

cout<< palindrome("Was it a car or a cat I saw?");


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Search results for 'How to make a palindrome program that reads an array of characters and determines if it's a palindrome?' (Questions and Answers)
5
replies
C++ programming, use of pointers?
started 2009-07-25 14:17:57 UTC
programming & design
Loading...