Checking palindromes using stacks

i have a task which i have to use a class for as well as any data structure, i chose to use stacks a Anyway my problem is i tried to do the following code to check if a word is a palindrome. The programs compile but with wrong output.the program either gives me an infinite loop or wrong output.
I did not include the Stack class decleration

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
85
86
87
88
89
90
91
92
93
94
95
96
//check if string is a palindrome or not
#include "Stack.h"
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
		int count = 0;
		int length;
		string word;
		char x, c;
		cout << "Please enter a word\n";
		getline(cin, word);
		length = word.length() + 1;
	
		char  * arr = new char[length];
		
		strcpy(arr, word.c_str());
		
		Stack palindrome(length);

	while(count <= (length - 1))
	{
		c = arr[count];
		palindrome.push(c);
		count++;
	}
	
	count = 0;
	while(count <= (length - 1))
	{
			palindrome.pop(x);
			if(x != arr[count])
			{
				cout << "The string is not a palindrome\n";
				return 0 ;
			}
			count++;
		
	}
	cout << "The string is a palindrome\n";	
	
	delete [ ] arr;
	return 0;
	
}
}

#include "Stack.h"
#include <string>

Stack::Stack(int len)
{
	stackArray = new char[len];
	stackSize = len;
	top = -1;
}

Stack::~Stack()
{
delete [] stackArray;
}

void Stack::push(char str)
{
	top++;
	stackArray[top] = str;

}

void Stack::pop(char &str)
{
	if(isEmpty())
	{
		cout <<"No string\n";
	}
	else
	{
		str = stackArray[top];
		top--;
	}
}

bool Stack::isEmpty() const
{
	bool status;
	if(top == -1)
		status = true;
	else
		status = false;
	
	return status;
}


Your help will be highly appreciated.
Last edited on
give me the whole code :D?
so i can complite it xd
I will show how it could be done with using std::stack and you can use it as an example

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
#include <iostream>
#include <string>
#include <stack>

int main()
{
   std::cout << "Enter a word: ";

   std::string word;
   std::cin >> word;

   std::stack<char> st;

   for ( char c : word ) st.push( c );

   bool is_palindrome = true;

   for ( auto it = word.begin(); is_palindrome && !st.empty(); ++it )
   {
      is_palindrome = *it == st.top();
      st.pop();
   }

   std::cout << "Word \"" << word << "\" is "
             << ( is_palindrome ? "" : "not " )
             << "a palindrome."
             << std::endl;
} 

Last edited on
The valid range for indices is [0..string::length[. Not [1..string::length].

word[count] returns one character. Thus, x is a one-letter string. x[count] on line 27 is an error.

Stack::pop. Argument str is by value. I bet it should be by reference.


Food for thought:
What if you push only first length/2 into the stack and compare only the last length/2 of the word?
Last edited on
Syntax error:
for ( char c : word ) st.push( c );

@topnik1

Syntax error:
for ( char c : word ) st.push( c );


You should use a compiler that supports C++ 11 features.
You can test the code on-line at www.ideone.com

This statement can be substituted for

for ( std::string::size_type i = 0; i < word.size(); i++ ) st.push( word[i] );
Last edited on
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
#include <iostream>
#include <string>
#include <stack>

int main()
{
   std::cout << "Enter a word: ";

   std::string word;
   std::cin >> word;

   std::stack<char> st;
   char c;
   for ( int i=0 ;i<word.size(); i++  ){
	c=word.at(i);
	   st.push( c );
   }

   bool is_palindrome = true;

   for ( auto it = word.begin(); is_palindrome && !st.empty(); ++it )
   {
      is_palindrome = *it == st.top();
      st.pop();
   }

   std::cout << "Word \"" << word << "\" is "
             << ( is_palindrome ? "" : "not " )
             << "a palindrome."
             << std::endl;
} 

I fixed it
Also the statement

for ( auto it = word.begin(); is_palindrome && !st.empty(); ++it )

can be simplifiied

for ( auto it = word.begin(); is_palindrome; ++it )
ok :D
i just fix the errors u can do what u want with the rest xD
i prefer
http://rextester.com/runcode
Last edited on
Tnx for all your help, problem is we have not used the stack template yet (#include <stack>). Does anyone know how to do when you doing your own stack class (<include> "Stack.h")

my stack class declaration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef STACK_H
#define STACK_H
#include <iostream>
#include <string>
using namespace std;
class Stack
{
	
	private:
		char *stackArray;
		int stackSize;
		int top;
	public:
		
		//constructor
		Stack(int);
		//frees dynamically allocated memory
		~Stack();
		void push(char);
		void pop(char&);
		bool isEmpty() const;

};
#endif 
You shall add to your class member function top() that the top most element of the stack will be accessible.
EDIT: I am sorry I diid not see that the parameter of pop is defined as reference.So I did not see any problem. Please investigate my example. You should do minor changes of it.
Last edited on
Topic archived. No new replies allowed.