#include<iostream>
#include<string>
#include<stack>
#include<queue>
usingnamespace 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;
}
elseif( 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?
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);
elsebreak;
}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.
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
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
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();
}
boolis_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 ?