Palindrome using stack

I've made my program become more advance due to my last thread using que & stack
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
#include<iostream>
#include<string>
#include<stack>
#include<queue>

using namespace std;

int main(){
	stack<char> s;
	queue<char> q;
	string str;
	int i = 0;
	char ch;

	cout << "Enter a text in lowercase: "; 
	getline(cin, str);
	cout << endl;

	for ( i = 0 ; i < str.length(); i++){
		ch = str[i];
		q.push(ch);
		s.push(ch);
	}

	while(!q.empty() && !s.empty()){
		if(q.front() != s.top()){
			cout << "The word " << str << " is not a palindrome" << endl;
			break;
		}
		else if( q.front() = s.top() ){
			i++;
			q.pop();
			s.pop();
			if( i = str.length()){
				cout << "The word " << str << " is a palindrome" << endl;
				break;
			}
		}
	}
	system( "pause" );
	return 0;
}


but my program only compare the last letter and first letter.
I read my slides but not like iterator . got begin and end . so it can compare using loop .
but how to do it through stack and queue?
In this statement

else if( q.front() = s.top() ){

s.top is assigned to q.front.:) But maybe it is a more advanced approach, who does know?
Also if the control will be passed inside tis code block

1
2
		else if( q.front() = s.top() ){
			i++;


i will become equal to str.length() + 1
Last edited on
As I said you early the more simple way though not so advanced as your is to use the following check

1
2
3
4
5
std::vector<char> v;

// some stuff to fill the vector

if ( v == std::vector<char>( v.rbegin(), v.rend() ) ) std::cout << "The word is polindrome\n";
Last edited on
do you mean?
1
2
else if( q.front() = str.length() + 1 ){
			i++;


is still fail to work..
I do not see any sense of using i in this loop. As for your new statement it is also invalid.
ya i know. but my question want me to do with queue and stack.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
stack<char> s;
	queue<char> q;
	vector<char> v;
	int i = 0;
	char InputChar;


	//Enter sequence until user eof 
	do{
		//User input
		cout << "Enter string/sequence : ";
		cin  >> InputChar;

		//To let program won't repeat the same letter for the last chararacter
		//If without if , output : 123455
		//If statement  , output : 12345
		if( cin.good() )
			v.push_back(InputChar);
		else
			break;
	}while( !cin.eof() );


I'm using your way to fill up the vector


but now i've to use stack and queue to compare?
what's the algo?
What is the great sense to use a queue and a stack simultaneously? It seems that you should write two separate programs one that uses a queue and other that uses a stack.
but i see some people do using only 1 program.

http://answers.yahoo.com/question/index?qid=20091109124158AApRS6n


the link .
but i wan to know it. using my program..
It's ok to use the container to check the different behaviors.

Your problem is currently on line 30 and 34. You're using the assignment operator (... = ...) instead you certainly want to use the comparison operator (... == ...)

On line 30: You can omit if( q.front() = s.top() ) (not the else in front of it). Try to understand why it is so...

On line 31: You don't reset i to 0 after you used it in the loop on line 19
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include <vector>
#include<string>
#include<stack>
#include<queue>

using namespace std;

int main(){
	stack<char> s;
	queue<char> q;
	
	int i = 0;
	char InputChar;


	//Vector holds an object type char
	vector<char> v;
	istream::int_type c; 
  
	cout << "Enter a sequence of characters: "; 
	while ( ( c = cin.get() ) != EOF ) { 
		v.push_back( c ); 
	}

	if ( !v.empty() && v.back() == '\n' ) 
		v.pop_back(); 

	vector<char> v1(v.begin(),v.end());
	vector<char> v2(v.rbegin(),v.rend());

	for( i = 0 ; i < v.size() ; i++ ){
		s.push(v1[i]);
	}

	for( i = 0 ; i < v.size() ; i++ ){
		q.push(v2[i]);
	}

	if ( !v.empty() && v.back() == '\n' ) 
		v.pop_back(); 

	int i = 0; 
	while (  i < v.size() && q.front() == s.top() ) 
		i++; 
  
	cout << "The word "; 
  
	for ( char c : v ) 
		cout << c; 
	cout << " is" << ( i == v.size() ? "" :  " not" ) << " a polindrome." << endl;


here is the current code i done so far.

i having problem at my line 49 . as this line is teach by @vlad . any wrong at here?
line 49 is a language extension for C++11. You compiler likely doesn't support that as of yet.

You introduced a lot more errors now:

line 43: i is already defined (on line 13)
line 44: without pop() it always compares the very first element

line 30: imagine both q and s contain "abc". q.front() retrieves 'c'. s.top() retrieves 'a'. You used to use that to check whether it's a palindrome. But now you feed q with the reverse (cba) hence q.front() and s.top() both retrieve always the same

line 22: I have really no idea how EOF may appear within the cin stream
do you mean that i should change instead

vector<char> v2(v.rbegin(),v.rend());

to v2(v.begin() , b.end() )?

so what should i change for line 49?
so how many lines total i should change? getting blur a bit.

err . EOF that is no problem already . as my question stated it needed
Forget v1 and v2. Use v to feed s and q

so what should i change for line 49?
If your compiler doesn't support that replace it with an ordinary for:
1
2
  for ( i = 0; i < v.size(); i++) 
		cout << v[i]; 


within the while loop on line 44 you need to call pop() for both s and q

Line 43 doesn't give you a compiler error?
Last edited on
after i change the for() . only the error come out .
what mean use v to feed s and q?

isn't vector<char> v = q.front();?

something like that?


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

using namespace std;

int main(){
	
	int i = 0;
	char InputChar;


	//Vector holds an object type char
	vector<char> v;
	istream::int_type c; 
  
	cout << "Enter a sequence of characters: "; 
	while ( ( c = cin.get() ) != EOF ) { 
		v.push_back( c ); 
	}

	if ( !v.empty() && v.back() == '\n' ) 
		v.pop_back(); 

	if ( !v.empty() && v.back() == '\n' ) 
		v.pop_back(); 

	stack<char> s;
	queue<char> q;

	while (  i < v.size() && q.front() == s.top() ){
		s.pop();
		q.pop();
		i++;
	}
  
	cout << "The word "; 
  
	for( i = 0 ; i < v.size() ; i++){
		cout << v[i];
	}

	cout << " is" << ( i == v.size() ? "" :  " not" ) << " a polindrome." << endl;

	system( "pause" );
	return 0;
}


having an error said that my
deque iterator are not dereferencable ?
Last edited on
with feed i mean providing data. You still need a loop to provide the data to s and q:

1
2
3
4
	for( i = 0 ; i < v.size() ; i++ ){
		s.push(v[i]);
		q.push(v[i]);
	}


Instead of this:
1
2
3
4
5
	if ( !v.empty() && v.back() == '\n' ) 
		v.pop_back(); 

	if ( !v.empty() && v.back() == '\n' ) 
		v.pop_back(); 
use a loop:
1
2
	while ( !v.empty() && v.back() == '\n' ) 
		v.pop_back();

Why don't you use your remove_if approach with isalnum:

http://www.cplusplus.com/reference/clibrary/cctype/isalnum/

[EDIT]
deque iterator are not dereferencable ?
runtime or compile time error?
Last edited on
it don't hv error when i push the s and q.
but now.

all my result are palindrome.
something wrong with my if else statement to compare the v[i]?

for my line 45?
why i get the result is a palindrome only?
The problem is that you use i in certain loops. when you check it on line 45 it is not longer what you think it is.

On line 33: use a for loop:
1
2
3
4
5
6
7
	for ( i = 0;  i < v.size() && q.front() == s.top(); i++ ){
		s.pop();
		q.pop();
	}
	bool is_pal = (i == v.size()); // here i is what you think. later not longer
...
	cout << " is" << ( is_pal ? "" :  " not" ) << " a polindrome." << endl; // do not use i here 


It'd be better if you use the index of a loop just internally (for(int i = 0; ...) and create variables for your decisions
i did it . after i just change to for loop . i get it.
because if
i++ . the original value will change also right?
so when go to the if statement . the value i is already changed ?
Topic archived. No new replies allowed.