473,549 Members | 4,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_cha r);

while (cur_char != '\n')
{
the_stack.push (cur_char));
the_queue.enque ue (cur_char);
cin.get (cur_char);
}
is_pal = true;

while(not the_queue.is_em pty() and is_pal)
{
the_stack.get_t op(tos, s);
the_queue.get_f ront(foq, s);

the_stack.pop(s );
the_queue.deque ue(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 13847

"Lorin Leone" <le********@hot mail.com> wrote in message
news:92******** *************** ***@posting.goo gle.com...
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_cha r);

while (cur_char != '\n')
{
the_stack.push (cur_char));
the_queue.enque ue (cur_char);
cin.get (cur_char);
}
is_pal = true;

while(not the_queue.is_em pty() and is_pal)
{
the_stack.get_t op(tos, s);
the_queue.get_f ront(foq, s);

the_stack.pop(s );
the_queue.deque ue(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::iterato r 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(st d::equal_to<cha r>(), ' ')

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********@hot mail.com> a écrit dans le message de
news:92******** *************** ***@posting.goo gle.com...
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::iterato r 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************ ****@RREEMMOOVV EEliwest.at> wrote in message
news:10******** *******@news.li west.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::iterato r 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
11410
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: The input is the string whose largest palindrome substring should be
4
4582
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"); mypal.Ispalindrome(); // should return true i tried to do the code, but im having problems in comlpeting it, I hope that some one can help me know...
3
6120
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 which the letters spell the same message when written forward and backward (with whitespaces and punctuations not considered) is called a palindrome. ...
2
2781
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 return It's a Palindrome! and if I enter 33636 or 11214, it returns It's not a Palindrome!
2
13677
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 not work and i was woundering if any one could help. Thanks Private Sub cmdStart_Click() Dim inputWord As String Dim reverseWord As String...
20
13369
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
3496
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 result to the user on another page (include the original string that they input in the displayed output). Disregard the case of letters, as well as...
3
5990
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 clue on how to do this. I was given advice that I'd need to use arrays, but for javascript I have no idea how to work with them. I read some stuff in...
2
4132
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. Your program should ignore blanks, punctuation, and lettercase Your program should output what the user entered as well as the reversed line. ...
0
7526
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7455
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7962
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7480
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5373
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.