473,405 Members | 2,334 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 string using stack and queue C++

Hi, I am supposed to write a program that would identify whether a word is a palindrome string or not. I am having troubles with the spacing and punctuations.
my code compiles and runs fine except if i have space or any punctuations in between the words it doesnt work. can anyone help me out please?

here is my code

int main()
{

queue <string> Q;
stack <string> S;
string str;
int i;

queue<string>::size_type mismatches = 0;

cout<< "Enter a word : ";
getline(cin, str, '\n');;

for (i = 0; i < str.length(); i++)
{

Q.push(str[i]);
S.push(str[i]);
}

while ((!Q.empty( )) && (!S.empty( )))
{
if (Q.front( ) != S.top( ))
++mismatches;
Q.pop( );
S.pop( );
}

if (mismatches == 0)
cout << "That is a palindrome." << endl;
else
cout << "That is not a palindrome." << endl;

return 0;


}
Apr 26 '09 #1
4 18639
JosAH
11,448 Expert 8TB
In your first loop where you push every character of the string to your queue and stack, don't push a character when it is a white space character or a punctuation character. The rest of the code can stay as it is.

kind regards,

Jos
Apr 26 '09 #2
what do you mean by that ? should i write a loop inside that loop so that whenever there is a punctuation it doesnt push it through
Apr 26 '09 #3
JosAH
11,448 Expert 8TB
@kpalakode
There's no need to write yet another loop: for every character c in your string only push it if isalpha(c) returns true (see the type.h or ctype header file).

kind regards,

Jos
Apr 27 '09 #4
newb16
687 512MB
Btw, why did *whoeverwrotethiscode* use string in queue and stack if there are only chars held later?
Apr 27 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: Runic911 | last post by:
Does anyone know how i can fix my Palindrome program? s = raw_input('Enter a String: ') punctuation = '%$!*.,-:? ;()\'\"\\' i = 0 h = 0 t = 0 p = '' z = 0 while s!= ' ':
4
by: Lorin Leone | last post by:
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...
7
by: Allan Bruce | last post by:
I have had a look through the FAQ and found that if I am using a class template then I need an argument list. I have tried to add this but it is not quite working - i.e. it doesnt compile. My...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
4
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class...
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:...
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?
1
by: 1051109210 | last post by:
class MyStack{ public: MyStack(int sz=10); ~MyStack(); char pop(); void push(char ch); bool isEmpty(); bool isFull(); private: char *value;
7
by: xiaolim | last post by:
hi, sorry to disturb again, currently i have an assignment on stacks and as show in the the title, i need to use stacks to determine palindrome. i've done part of my code, here it is: //...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.