fork download
  1. #include <iostream>
  2. #include <queue>
  3. #include <stack>
  4. #include <cctype> // for tolower
  5. #include <string>
  6. using namespace std;
  7.  
  8. // -------------------------
  9. // Palindrome Checker Function
  10. // -------------------------
  11. void checkPalindrome(const string& input) {
  12. // Reject input with spaces
  13. if (input.find(' ') != string::npos) {
  14. cout << "Please enter only one word without spaces.\n";
  15. return;
  16. }
  17.  
  18. stack<char> s;
  19. queue<char> q;
  20.  
  21. // Insert characters into stack and queue
  22. for (char c : input) {
  23. char lower = tolower(c); // تحويل الحرف إلى صغير
  24. s.push(lower);
  25. q.push(lower);
  26. }
  27.  
  28. bool isPalindrome = true;
  29. while (!s.empty()) {
  30. if (s.top() != q.front()) {
  31. isPalindrome = false;
  32. break; // إذا حدث اختلاف، لا داعي لاستكمال المقارنة
  33. }
  34. s.pop();
  35. q.pop();
  36. }
  37.  
  38. if (isPalindrome)
  39. cout << "Palindrome\n";
  40. else
  41. cout << "Not palindrome\n";
  42. }
  43.  
  44. // -------------------------
  45. // Main
  46. // -------------------------
  47. int main() {
  48. string word;
  49. cout << "\nEnter a word to check if it's a palindrome: ";
  50. cin.ignore(); // مهم علشان نتفادى مشكلة getline بعد cin
  51. getline(cin, word);
  52.  
  53. checkPalindrome(word);
  54.  
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0.01s 5288KB
stdin
akka
stdout
Enter a word to check if it's a palindrome: Not palindrome