Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include "Stack.h"
  2.  
  3. #include <iostream>
  4.  
  5. #include <string>
  6.  
  7. #include <cstring>
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12.  
  13. {
  14.  
  15. int count = 0;
  16.  
  17. int len;
  18.  
  19. string w;
  20.  
  21. char a, c;
  22.  
  23. cout << "Please enter a word\n";
  24.  
  25. getline(cin, w);
  26.  
  27. len = w.len() + 1;
  28.  
  29. char * arr = new char[len];
  30.  
  31. strcpy(arr, w.c_str());
  32.  
  33. Stack palindrome(len);
  34.  
  35. while(count <= (len - 1))
  36.  
  37. {
  38.  
  39. c = arr[count];
  40.  
  41. palindrome.push(c);
  42.  
  43. count++;
  44.  
  45. }
  46.  
  47. count = 0;
  48.  
  49. while(count <= (len - 1))
  50.  
  51. {
  52.  
  53. palindrome.pop(a);
  54.  
  55. if(a != arr[count])
  56.  
  57. {
  58.  
  59. cout << "The string is not a palindrome\n";
  60.  
  61. return 0 ;
  62.  
  63. }
  64.  
  65. count++;
  66.  
  67. }
  68.  
  69. cout << "The string is a palindrome\n";
  70.  
  71. delete [ ] arr;
  72.  
  73. return 0;
  74.  
  75. }
  76.  
  77. }
  78.  
  79. #include "Stack.h"
  80.  
  81. #include <string>
  82.  
  83. Stack::Stack(int len)
  84.  
  85. {
  86.  
  87. stackArray = new char[len];
  88.  
  89. stackSize = len;
  90.  
  91. top = -1;
  92.  
  93. }
  94.  
  95. Stack::~Stack()
  96.  
  97. {
  98.  
  99. delete [] stackArray;
  100.  
  101. }
  102.  
  103. void Stack::push(char str)
  104.  
  105. {
  106.  
  107. top++;
  108.  
  109. stackArray[top] = str;
  110.  
  111. }
  112.  
  113. void Stack::pop(char &str)
  114.  
  115. {
  116.  
  117. if(isEmpty())
  118.  
  119. {
  120.  
  121. cout <<"No string\n";
  122.  
  123. }
  124.  
  125. else
  126.  
  127. {
  128.  
  129. str = stackArray[top];
  130.  
  131. top--;
  132.  
  133. }
  134.  
  135. }
  136.  
  137. bool Stack::isEmpty() const
  138.  
  139. {
  140.  
  141. bool status;
  142.  
  143. if(top == -1)
  144.  
  145. status = true;
  146.  
  147. else
  148.  
  149. status = false;
  150.  
  151. return status;
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement