Advertisement
rado_dimitrov66

Untitled

Apr 15th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. struct Steck {
  8. char data;
  9. Steck* next;
  10. }*current;
  11.  
  12.  
  13. void removeword()
  14. {
  15. while (current)
  16. {
  17. Steck* p = current;
  18. current = current->next;
  19. delete p;
  20. }
  21. }
  22.  
  23. void addWord()
  24. {
  25. string word;
  26. cout << "Enter word: ";
  27. cin >> word;
  28.  
  29. removeword();
  30.  
  31. if (word.length() > 0)
  32. {
  33. for (int i = word.length(); i >= 0; i--)
  34. {
  35. Steck* p = current;
  36. current = new Steck;
  37. current->data = word[i];
  38. current->next = p;
  39. }
  40.  
  41. }
  42. else { cout << "No word"; }
  43.  
  44. }
  45.  
  46. void printwords()
  47. {
  48. Steck* p = current;
  49. while (p->next)
  50. {
  51. cout << p->data;
  52. p = p->next;
  53. }
  54. cout << "" << endl;
  55. }
  56.  
  57.  
  58. bool checkForPalyndrom()
  59. {
  60. string word = "";
  61. Steck* p = current;
  62.  
  63.  
  64. while (p->next)
  65. {
  66. word += p->data;
  67. p = p->next;
  68. }
  69.  
  70. for (int i = 0; i < word.length(); i++)
  71. {
  72. if (word[i] != word[word.length() - i - 1])
  73. {
  74. return false;
  75. }
  76. }
  77.  
  78. return true;
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85. int main()
  86. {
  87. short choice;
  88.  
  89. do
  90. {
  91. cout << "[1] Add word\n[2] Remove word \n[3] Print words\n[4] Check for palyndrom\n[0] Exit\n";
  92. cin >> choice;
  93.  
  94. switch (choice)
  95. {
  96. case 1:
  97. system("cls");
  98.  
  99. addWord();
  100.  
  101. choice = -1;
  102.  
  103. break;
  104. case 2:
  105.  
  106. system("cls");
  107.  
  108. if (!current) {
  109. cout << "No words to remove\n";
  110. }
  111. else {
  112. removeword();
  113. }
  114.  
  115.  
  116. choice = -1;
  117.  
  118. break;
  119.  
  120. case 3:
  121.  
  122. system("cls");
  123.  
  124. if (!current) {
  125. cout << "No elements to print\n";
  126. }
  127. else {
  128. printwords();
  129. }
  130.  
  131. choice = -1;
  132.  
  133. break;
  134.  
  135. case 4:
  136.  
  137. system("cls");
  138.  
  139.  
  140. if (!current) {
  141. cout << "no word for check\n";
  142. }
  143. else {
  144.  
  145.  
  146. if (checkForPalyndrom())
  147. {
  148. cout << "Word is palyndrom\n";
  149. }
  150. else
  151. {
  152. cout << "Word is not palyndrom\n";
  153. }
  154.  
  155. }
  156.  
  157. choice = -1;
  158.  
  159. break;
  160. default:
  161. break;
  162. }
  163.  
  164. } while (choice < 0 || choice > 4);
  165.  
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement