Advertisement
skb50bd

Palindrome (Stack)

Dec 11th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. Check if a Word is Palindrome or Not using Stack
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <stack>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.     string word;
  11.     stack <char> S;
  12.     stack <char> T;
  13.     stack <char> temp;
  14.  
  15.     cin >> word;
  16.  
  17.     for(int i = 0; word[i]; i++)
  18.         S.push(word[i]);
  19.  
  20.     while(!S.empty()) {
  21.         temp.push(S.top());
  22.         T.push(S.top());
  23.         S.pop();
  24.     }
  25.  
  26.     while(!temp.empty()) {
  27.         S.push(temp.top());
  28.         temp.pop();
  29.     }
  30.  
  31.     while(!S.empty()) {
  32.         if(S.top() != T.top()) break;
  33.         S.pop();
  34.         T.pop();
  35.     }
  36.  
  37.     if(S.empty()) cout << word << " is Palimdrome" << endl;
  38.     else cout << word << " is Not Palindrome" << endl;
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement