Palindrome (HELP)

L

Lorin Leone

Can anyone help me modify the program so that it recognizes strings
like "Anna" as palindromes. To make the program "case-insensitive."
using the built-in C++ function "toupper". and so that it recognizes
strings like
"race car" as palindromes, have the program ignore spaces in the input
string.

Here is my code:


#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

#include "Stack.h"
#include "queue.h"

int main()
{

Stack the_stack;
Queue the_queue;
char cur_char;
char tos;
char foq;
bool s;
bool is_pal;

cout << "input string: ";
cin.get(cur_char);

while (cur_char != '\n')
{
the_stack.push (cur_char));
the_queue.enqueue (cur_char);
cin.get (cur_char);
}


is_pal = true;

while(not the_queue.is_empty() and is_pal)
{
the_stack.get_top(tos, s);
the_queue.get_front(foq, s);

the_stack.pop(s);
the_queue.dequeue(s);
if(tos != foq)

is_pal = false;

}

cout << endl;

if( is_pal )

cout << "String is a palindrome." << endl;

else

cout << "String is NOT a palindrome." << endl;


return 0;

}
 
C

Chris Theis

Lorin Leone said:
Can anyone help me modify the program so that it recognizes strings
like "Anna" as palindromes. To make the program "case-insensitive."
using the built-in C++ function "toupper". and so that it recognizes
strings like
"race car" as palindromes, have the program ignore spaces in the input
string.

Here is my code:


#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

#include "Stack.h"
#include "queue.h"

int main()
{

Stack the_stack;
Queue the_queue;
char cur_char;
char tos;
char foq;
bool s;
bool is_pal;

cout << "input string: ";
cin.get(cur_char);

while (cur_char != '\n')
{
the_stack.push (cur_char));
the_queue.enqueue (cur_char);
cin.get (cur_char);
}


is_pal = true;

while(not the_queue.is_empty() and is_pal)
{
the_stack.get_top(tos, s);
the_queue.get_front(foq, s);

the_stack.pop(s);
the_queue.dequeue(s);
if(tos != foq)

is_pal = false;

}

cout << endl;

if( is_pal )

cout << "String is a palindrome." << endl;

else

cout << "String is NOT a palindrome." << endl;


return 0;

}

Okay, first I'd suggest the following code which is more efficient to check
for a palindrome:

string Test;
cin >> Test;
bool bFlag = true;

string::iterator IterFw = Test.begin();
string::reverse_iterator IterBw = Test.rbegin();
while( *(IterFw++) == *(IterBw++) && IterFw != Test.end() ) {
bFlag = false;
}

if( !bFlag )
cout << "it's a palindrome" << endl;
else
cout << "it's NOT a palindrome" << endl;

Some more hints to solve your problem - before you feed the string to the
palindrom algorithm you have to do some preprocessing like stripping the
blanks. A possible but not very efficient way would be to go through the
string character by character and copy those that are not blanks into a new
string. Another way would be to consider the remove_if() function with a
predicate that should look like
std::bind2nd(std::equal_to<char>(), ' ')

In order to make lower case comparisons you'll have to modify the condition
of the while loop given above, a little bit. At the moment it compares
character by character taking upper and lower case into account. I guess
this should get you started. Otherwise try and post when you've got some
more trouble.

HTH
Chris
 
O

.oO LGV Oo.

Lorin Leone said:
Can anyone help me modify the program so that it recognizes strings
like "Anna" as palindromes. To make the program "case-insensitive."
using the built-in C++ function "toupper". and so that it recognizes
strings like
"race car" as palindromes, have the program ignore spaces in the input
string.

Here is my code:

'sounds like a homework assignment... if you wrote the code you provide, you
shouldn't have major difficulties to modify it in order to obtain the
desired behaviour...
 
M

M. Baumgartner

Chris said:
Okay, first I'd suggest the following code which is more efficient to check
for a palindrome:

string Test;
cin >> Test;
bool bFlag = true;

string::iterator IterFw = Test.begin();
string::reverse_iterator IterBw = Test.rbegin();
while( *(IterFw++) == *(IterBw++) && IterFw != Test.end() ) {
bFlag = false;
}

This code is more efficient than the original, but still could be done
better. You actually do twice the work that is necessary by comparing
until the end of the string. In fact, it would be enough to compare up
to the middle character.

In addition, setting the boolean flag inside the loop is obsolete. You
can distinguish the two cases just by testing which of the terms in the
and-expression led to leaving the loop.

M.
 
C

Chris Theis

M. Baumgartner said:
This code is more efficient than the original, but still could be done
better. You actually do twice the work that is necessary by comparing
until the end of the string. In fact, it would be enough to compare up
to the middle character.

In addition, setting the boolean flag inside the loop is obsolete. You
can distinguish the two cases just by testing which of the terms in the
and-expression led to leaving the loop.

M.

You're of course absolutely right. I just think that the OP is not a very
experienced programmer thus introducing to many nifty things might confuse
more than it helps :) This looks very much like an assignment and IMHO some
thinking and room for improvement should be left to the original author.

Cheers
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top