473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Palindrome (HELP)

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;

}
Jul 19 '05 #1
4 13834

"Lorin Leone" <le********@hotmail.com> wrote in message
news:92**************************@posting.google.c om...
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
Jul 19 '05 #2

"Lorin Leone" <le********@hotmail.com> a écrit dans le message de
news:92**************************@posting.google.c om...
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...
Jul 19 '05 #3
Chris Theis wrote:
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.

Jul 19 '05 #4

"M. Baumgartner" <ma****************@RREEMMOOVVEEliwest.at> wrote in message
news:10***************@news.liwest.at...
Chris Theis wrote:
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.


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
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: ramakrishnadeepak | last post by:
HI Everybody, I 've to submit a program on c.Can any one help me plz.........The problem is like this:: Write a program which computes the largest palindrome substring of a string. Input:...
4
by: outofmymind | last post by:
hi, im trying to solve the following question: Create a class responsible for determining whether a string is a palindrome. Show your test cases. Palindome mypal("bob"); ...
3
by: colinNeedsJavaHelp | last post by:
I am still having an exceptional amount of trouble with java. This is my new assignment, if anyone can help I would greatly appreciate it. I don't even know where to start. A word or phrase in...
2
by: Synapse | last post by:
aloha people! I need help in my java palindrome program. It's a 5-digit palindrome checker. The code below is running good but i got a few problems on it. If I enter 33633, 11211, 45554, it will...
2
by: xlilxmizzxinnocentx | last post by:
Hiya I was woundering if anyone could help me. A few weeks ago i started using vb 5.0 and now im trying to make a code to determine if a word is a palindrome or not. The code that i have tried dose...
20
by: Wabz | last post by:
Hello mates, Does anyone know how to write a function that tests if an integer is a palindrome in C language?
5
by: rubyhuang | last post by:
i'm a new perl learner, this is the first perl task i will do. please help me. The user can input a string, and then a script will check to see if the string is a palindrome or not, displaying the...
3
by: hl2ob | last post by:
Alright I'm still new to javascript. I was getting it pretty well, and getting everything alright untill this point. We have to make a program that test a 5 digit number as a palindrome. I have no...
2
by: bigtd08 | last post by:
help writing this palindrome program for my c++ class. HERE WHAT THE CODE SHOULD BE LIKE. Write a program that takes a line of input from the keyboard and check to see if that line is a palindrome....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.