0

I want to write a method to determine if a given string is a palindrome. E.g. "Madam I'm Adam", or "A man, a plan, a canal, Panama".

The prototype for the function is:

bool is_palindrome(char const * str)

I have a simple logic to check for equality by moving forward & backward from extreme ends of the string.
But, i would like to know how many efficient ways to do this ? All ideas welcome from C++ gurus..

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by jonsca
0

A hint: moving forwards and backwards and comparing will not yield detection of a palindrome in the strings you have there. You have the right idea you just need some preprocessing first.

Can you think of any other efficient ways to do it?

0

Jonsca: oh, do you mean removing non-alphanumeric characters as preprocessing ?
I can think of using STL strings & equal() algorithm .. any other ideas, please?

0

Jonsca: oh, do you mean removing non-alphanumeric characters as preprocessing ?
I can think of using STL strings & equal() algorithm .. any other ideas, please?

Yes, you need to pull out the apostrophes and commas, as they are not at the same position forwards and backwards.

STL strings will work with an iterator or an STL algorithm like reverse().

Are there any other STL containers that might be ideal for this? One that might help you reverse something?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.