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

Recommended Answers

All 3 Replies

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?

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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.