Palindrome using 3 stacks

Hi,
I want to use 3 stacks to check a word that user entered whether is it Palindrome or not.
I have written this code:
But I don't know how to use 3 stacks in "main".
I appreciate your help.
I don't want to use back and front.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "stdafx.h"
#include <iostream>
//#ifndef STACK_H
#define STACK_H
using namespace std;

const int SIZE=5;
enum {EMPTY=-1,FULL=SIZE-1};

class Stack
{
public:
	Stack();
	bool emptyStack();
	bool fullStack();
	void push(char c);
	char pop();
	char topElement();
private:
	char s[SIZE];
	int top;
};

Stack::Stack()
{
	top=EMPTY;
}
bool Stack::emptyStack()
{
	if (top==EMPTY)
		return true;
	else
		return false;
}
bool Stack::fullStack()
{
	if (top==FULL)
		return true;
	else
	return false;
}
void Stack::push(char c)
{
	if (!fullStack())
	{
			++top;
			s[top]=c;
	}
	else
		cout<<"is full"<<endl;
}
char Stack::pop()
{
	char c;
	if (!emptyStack())
	{
		c=s[top];
		--top;
		return c;
	}
	else
	{
		cout<<"The stack is empty! pop"<<endl;
		return '#';
	}
}
char Stack::topElement()
{
	if (!emptyStack())
		return s[top];
	else
	{
		cout<<"The stack is empty! topE"<<endl;
		return '#';
	}
}

int main()
{

	return 0;
}

Why exactly are you using a stack to check if a string is a palindrome? All you have to do is have one iterator from the beginning and the other from the back and if they do not equal they are not palindromes.


1
2
3
4
5
6
7
bool palindrome = true;
for(auto it = str.begin(), it2 = str.end()-1; it < it2 && palindrome; ++it , --it2)
//if the string is odd characters you can iterate while they are not equal
//but for evens they will not equal each other
{
    palindrome = *it == *it2;
}


http://coliru.stacked-crooked.com/a/7555f39c1f351802
+1 giblit solution

Not only are you using stacks, but why 3? Is this an assignment requirement or did you just decide to write a palindrome checker using stacks?
I want these codes I have written to be in my program.
and I want to use 3 stacks.
I don't want to use auto-back-front-begin-end.
Thank you
Last edited on
The question remains the same, why 3? Why not just 2? If you plan to use 3, then explain your reason or how you want to do it.

It can be done with 2 by having iterators (pointers) at the front and back of the string. Then as you move the iterators towards each other, you simply push the character at each iterator into the respective stack. In the end you start popping from each stack and comparing the values you get; if any of the values are not the same, then the string is not a palindrome
I don't wanna use pointers.
If you can do it with 2, that's fine.
Can even be done with one stack
http://ideone.com/S2HPpw

If you are looking for a challenging problem to use your stack ADT on, then try some algorithms that require the use of recursion:
https://www.artofproblemsolving.com/Wiki/index.php/Recursion
Topic archived. No new replies allowed.