Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Check if a Word is Palindrome or Not using Stack
- #include <iostream>
- #include <string>
- #include <stack>
- using namespace std;
- int main() {
- string word;
- stack <char> S;
- stack <char> T;
- stack <char> temp;
- cin >> word;
- for(int i = 0; word[i]; i++)
- S.push(word[i]);
- while(!S.empty()) {
- temp.push(S.top());
- T.push(S.top());
- S.pop();
- }
- while(!temp.empty()) {
- S.push(temp.top());
- temp.pop();
- }
- while(!S.empty()) {
- if(S.top() != T.top()) break;
- S.pop();
- T.pop();
- }
- if(S.empty()) cout << word << " is Palimdrome" << endl;
- else cout << word << " is Not Palindrome" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement