473,692 Members | 1,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

palindrome check programe using stack

18 New Member
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. // Write your name, student number, part-time/full time, degree here
  3. #include <string>
  4. #include <iostream>
  5. #define STACKSIZE 80
  6. #define TRUE 1
  7. #define FALSE 0
  8. using namespace std;
  9.  
  10. class Stack 
  11. {
  12.    public:
  13.           void StackInit();
  14.           int IsStackEmpty();
  15.           int IsStackFull();
  16.           int StackPush(char c);
  17.           char StackPop();
  18.           char ViewStackTop();
  19.    private:
  20.           char myStack[80];
  21.           int top;
  22. };
  23.  
  24. string pstring;
  25.  
  26. int main()
  27. {
  28.     // write your code here
  29.     cout << "This Is A Palindrome Check Programe" << endl;
  30.     cout << "Please Enter Anything For Palindrome Check" << endl;
  31.  
  32.     cin >> pstring;
  33.  
  34.  
  35.  
  36.  
  37.     system("pause");
  38.     return 0;
  39. }
  40.  
  41. void Stack::StackInit()
  42. {
  43.   top = -1;
  44. }
  45.  
  46. int Stack::IsStackEmpty()
  47. {
  48.   if (top == -1)
  49.     return TRUE;
  50.   return FALSE;
  51. }
  52.  
  53. int Stack::IsStackFull()
  54. {
  55.   if (top == (STACKSIZE - 1))
  56.     return TRUE;
  57.   return FALSE;
  58. }
  59.  
  60. int Stack::StackPush(char c)
  61. {
  62.   if (top == (STACKSIZE - 1))
  63.     return FALSE;
  64.   myStack[++top] = c;
  65.   return TRUE;
  66. }
  67.  
  68. char Stack::StackPop()
  69. {
  70.   if (top == -1)
  71.     return '\0';
  72.   return myStack[top--];
  73. }
  74.  
  75. char Stack::ViewStackTop()
  76. {
  77.   if (top == -1)
  78.     return '\0';
  79.   return myStack[top];
  80. }
  81.  
  82.  
  83.  
alright, i am stuck again, i need to ignore space, punct and digits, i know using isdigit / ispunct / isspace. however i ahh.. duno where to put them. hmmm, simple guide will do. thanks, meanwhile, i will be doing search on google as well. please help. thanks
Aug 16 '08 #1
7 22556
weaknessforcats
9,208 Recognized Expert Moderator Expert
It looks like you need an algorithm. All I see is some stack code, which is not complete.

Put a loop in main(). Create a stack and then parse the string from left to right pushing each letter or number onto the stack (use isalnum to determine a letter or a number).

Then create a second stack and oparse the string from right to left pushing each letter or number onto the stack.

Finally, in a third loop pop each stack and compare letters. If not equal, you do not have a palindrome. If equal, pop them both again. If the letters remain equal when the stacks are empty, you have a palindrome.

Note that the loops could be replaced by recursive calls. But since this might produce a lot of nested calls, loops are more efficient.

Then post agian.
Aug 16 '08 #2
xiaolim
18 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <iostream>
  6. #define STACKSIZE 80
  7. #define TRUE 1
  8. #define FALSE 0
  9. using namespace std;
  10.  
  11. class Stack 
  12. {
  13.    public:
  14.           void StackInit();
  15.           int IsStackEmpty();
  16.           int IsStackFull();
  17.           int StackPush(char c);
  18.           char StackPop();
  19.           char ViewStackTop();
  20.    private:
  21.           char myStack[80];
  22.           int top;
  23. };
  24.  
  25.  
  26. char input[100], output[100];
  27. Stack s;
  28.  
  29. int main()
  30. {
  31.     char c;
  32.     s.StackInit();
  33.     // write your code here
  34.     cout << "This Is A Palindrome Check Programe" << endl;
  35.     cout << "Please Enter Anything For Palindrome Check" << endl;
  36.     fflush(stdin);
  37.     cin.getline(input, 100);
  38.  
  39.     for (int i=0;i<strlen(input);i++)
  40.  
  41.  
  42.     //check input is upper and convert to lower
  43.     while (input[i])
  44.     {
  45.     c=input[i];
  46.     putchar (tolower(c));
  47.     i++;
  48.     }
  49.  
  50.     //check input have punct or space
  51.     if (isspace(input[i]))
  52.  
  53.  
  54.  
  55.     //pass converted string to a temp
  56.     c = input[i];
  57.  
  58.     //check input is only alpha and push to stack
  59.     if(isalpha(input[i]))   
  60.     s.StackPush(input[i]);
  61.     i++;
  62.  
  63.  
  64.     //compare 1st 2 char with last 2 char
  65.     if strcmp( ?????? )
  66.  
  67.     //output
  68.     cout << "The String " << c << "Is A Palindrome." << endl;
  69.  
  70.     else
  71.  
  72.     cout << "The String " << c << "Is Not A Palindrome." << endl;    
  73.  
  74.  
  75.  
  76.     system("pause");
  77.     return 0;
  78. }
  79.  
  80. void Stack::StackInit()
  81. {
  82.   top = -1;
  83. }
  84.  
  85. int Stack::IsStackEmpty()
  86. {
  87.   if (top == -1)
  88.     return TRUE;
  89.   return FALSE;
  90. }
  91.  
  92. int Stack::IsStackFull()
  93. {
  94.   if (top == (STACKSIZE - 1))
  95.     return TRUE;
  96.   return FALSE;
  97. }
  98.  
  99. int Stack::StackPush(char c)
  100. {
  101.   if (top == (STACKSIZE - 1))
  102.     return FALSE;
  103.   myStack[++top] = c;
  104.   return TRUE;
  105. }
  106.  
  107. char Stack::StackPop()
  108. {
  109.   if (top == -1)
  110.     return '\0';
  111.   return myStack[top--];
  112. }
  113.  
  114. char Stack::ViewStackTop()
  115. {
  116.   if (top == -1)
  117.     return '\0';
  118.   return myStack[top];
  119. }
  120.  
  121.  
  122.  
alright, i tried, i briefly list out what i should do.

1. StackInit.
2. promt user to enter anything.
3. check if is uppercase, if yes, convert to lowercase then move to next char
4. ignore all space and punct.
5. pass the converted string to another temp var.
6. check if is only alpha, if yes, push char in stack. if not, ignore go to next char
7. pop the last 2 char.
8. compare 1st 2 char with last 2 char, if is similar, then output result, if not, display msg.

so, quite alot of problem, maybe someone can help me check out n tell me where i go wrong? i keep can't compile on the "tolower" part. thanks alot
Aug 18 '08 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
OK, I see where you are trying to build a stack of the letters as you go from left to right.

Where is the stack for the letters when you go from right to left? I see only one stack.

When you get to strcmp(????) both stacks need to be complete. You then pop from eaxch stack and if the two stacks are identical, you have a palindrome.
Aug 18 '08 #4
xiaolim
18 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2. int main()
  3. {
  4.     char input[100], output[100];
  5.     Stack s1;
  6.     Stack s2;
  7.     Stack s3;
  8.     s1.StackInit();
  9.  
  10.     // write your code here
  11.     cout << "This Is A Palindrome Check Programe" << endl;
  12.     cout << "Please Enter Anything For Palindrome Check" << endl;
  13.     fflush(stdin);
  14.     cin.getline(input, 100);
  15.  
  16.  
  17.     for (int i=0;i<strlen(input);i++)
  18.  
  19.     {
  20.      if (isupper(input[i]))
  21.      input[i]=tolower(input[i]);
  22.  
  23.      if (isspace(input[i]))
  24.      i++;
  25.  
  26.      if (ispunct(input[i]))
  27.      i++;
  28.  
  29.      if (isalpha(input[i]))
  30.      s1.StackPush(input[i]) & s2.StackPush(input[i]);
  31.  
  32.      }
  33.  
  34.  
  35.  
  36.       cout << "Reverse String: ";
  37.       while (!s1.IsStackEmpty())
  38.       cout << s1.StackPop();
  39.       cout << "" << endl;
  40.  
  41.  
  42.  
  43.     system("pause");
  44.     return 0;
  45. }
  46.  
  47.  
alright, i' ve made some changes in my code. As shown in the code, i declared 3 stack, then i push input to stack1 & stack2 and get them back in reverse order. so now i want to push the reversed string to stack3 and then do a compare if they matches or not. so how do i push them back?
Aug 19 '08 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
I don't think you've got the idea yet.

Take the palimdrome: Madam, I'm Adam.

First, convert to lower case: madam i'm adam
Second: Replace all punctuation with a space: madam i m adam
Third: Create two stacks.
Fourth: Stack one push from the left to right
m
a
d
a
m
i
m
a
d
a
m

Fifth:Stack two push from the right to left:
m
a
d
a
m
i
m
a
d
a
m

Now can you see the two stacks are the same? You test this by popping each stack and comparing letters. If the letters match, pop again and repeat. When the stacks are empty and the letters always matchedm, then you have a palinrome.

There's no third stack.
Aug 20 '08 #6
hassanqbl67
1 New Member
#include<iostre am>
using namespace std;
int main()
{
int first;
int last;
first=0;
char arr[100];
cout<<"\t\t**** ******Palindrom e**********"<<e ndl<<endl<<endl ;
cout<<"Enter a word to check wether it is a Palindrome: "<<endl<<en dl;
cin>>arr;
last=strlen(arr )-1;
if(arr[first]==arr[last])
{

cout<<"It is a Palindrome"<<en dl;
while(first<100 )
{
first ++;
last --;
}
}
else
{
cout<<"It is NOT a Palindrome"<<en dl;
}
system("PAUSE") ;
return 0;
}
Apr 2 '10 #7
newb16
687 Contributor
Dug up thread is dug up.
Apr 2 '10 #8

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

Similar topics

24
13006
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
13853
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 recognizes strings like "race car" as palindromes, have the program ignore spaces in the input string. Here is my code:
32
11430
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
20
13391
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
2012
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;
5
3504
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 spaces and any punctuation symbols in the sentence (use Perl string routines to deal with these...
2
4138
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. Sample output: Please enter a phrase, and I will tell you if it is a palindrome. >> Madam I'm Adam
8
4875
by: chungjoel | last post by:
Hi, i'm new to C programming and i could use some help with an assignment. i would like to store a word in a linked list, with each letter of the word in a separate node of the linked list. i would then like to traverse the linked list to check whether the word is a palindrome. the nodes in the palindrome should be double pointer nodes. i'm not quite sure how to start here, any assistance will be appreciated.
4
18691
by: kpalakode | last post by:
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;
0
8600
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8535
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9079
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8954
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8794
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4322
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2234
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.