sidrs

FDS (LV) Ass 4B - Stack and Strings

Sep 2nd, 2024 (edited)
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Stack {
  6.     int tos;
  7.     char *stack;
  8.     int size;
  9. public:
  10.     Stack() {
  11.         stack = new char[1];
  12.         tos = 0;
  13.     }
  14.  
  15.     Stack(int size) {
  16.         this->size = size;
  17.         stack = new char[size];
  18.         tos = 0;
  19.     }
  20.  
  21.     void push(char ch) {
  22.         if ( tos > size - 1){
  23.             cout << "STACK OVERFLOW! Cannot push " << ch << endl;
  24.         } else if (tos == size - 1) {
  25.             stack[tos] = ch;
  26.         } else if (tos < size - 1 && tos >= 0) {
  27.             stack[tos++] = ch;
  28.         }
  29.     }
  30.  
  31.     void display() {
  32.         for (int i = 0; i <= tos; i++){
  33.             cout << stack[i];
  34.         }
  35.         cout << endl;
  36.     }
  37.  
  38.     void reverse() {
  39.         for (int i = tos; i >= 0; i--){
  40.             cout << stack[i];
  41.         }
  42.         cout << endl;
  43.     }
  44.  
  45.     void palindrome() {
  46.         bool isOK = true;
  47.         for (int i = 0, j = tos; i <= tos/2, j >= tos/2; i++, j--) {
  48.             if (stack[i] == stack[j]) {
  49.                 isOK = true;
  50.             } else if (stack[i] != stack[j]){
  51.                 isOK = false;
  52.                 break;
  53.             }
  54.         }
  55.        
  56.         if (isOK) cout << "GIVEN STRING IS PALINDROME!" << endl;
  57.         else if (!isOK) cout << "GIVEN STRING IS NOT PALINDROME!" << endl;
  58.     }
  59.  
  60.     ~Stack(){
  61.         delete[] stack;
  62.     }
  63. };
  64.  
  65. int main(){
  66.     string str;
  67.     cout << "Enter a string: ";
  68.     cin >> str;
  69.  
  70.     Stack ds(str.size());
  71.  
  72.     for (int i = 0; str[i] != '\0'; i++) {
  73.         ds.push(str[i]);
  74.     }
  75.  
  76.     cout << "\nDISPLAYING STACK CONTENTS: " << endl;
  77.     ds.display();
  78.  
  79.     cout << "\nDISPLAYING STACK CONTENTS (REVERSED): " << endl;
  80.     ds.reverse();
  81.    
  82.     cout << "\nPALINDROME CHECK: " << endl;
  83.     ds.palindrome();
  84.  
  85.  
  86. }
  87.  
  88. /* OUTPUT:
  89.  
  90. student@student:~/Documents/LAB_SB57/FDS/stringStack$ ./main.exe
  91. Enter a string: ferrari
  92.  
  93. DISPLAYING STACK CONTENTS:
  94. ferrari
  95.  
  96. DISPLAYING STACK CONTENTS (REVERSED):
  97. irarref
  98.  
  99. PALINDROME CHECK:
  100. GIVEN STRING IS NOT PALINDROME!
  101.  
  102.  
  103.  
  104. student@student:~/Documents/LAB_SB57/FDS/stringStack$ ./main.exe
  105. Enter a string: hannah
  106.  
  107. DISPLAYING STACK CONTENTS:
  108. hannah
  109.  
  110. DISPLAYING STACK CONTENTS (REVERSED):
  111. hannah
  112.  
  113. PALINDROME CHECK:
  114. GIVEN STRING IS PALINDROME!
  115.  
  116. */
  117.  
  118.  
Advertisement
Add Comment
Please, Sign In to add comment