<iostream> <stdexcept> <string>template <typename T>class Stack{public: Stack() : m_top(-1) { } bool isEmpty() const { return m_top == -1; } bool isFull() const { return m_top == CAPACITY - 1; } void push(const T& element) { if (isFull()) { throw std::overflow_error("Stack is full"); } ++m_top; m_data[m_top] = element; } T pop() { if (isEmpty()) { throw std::underflow_error("Stack is empty"); } return m_data[m_top--]; } const T& peek() const { if (isEmpty()) { throw std::underflow_error("Stack is empty"); } return m_data[m_top]; }private: static constexpr int CAPACITY = 100; int m_top; T m_data[CAPACITY];};int main(){ Stack<char> characters; std::string input; std::string reversed; std::cout << "Enter a word: "; std::cin >> input; for (char character : input) { characters.push(character); } while (!characters.isEmpty()) { reversed += characters.pop(); } if (input == reversed) { std::cout << "The word is a palindrome.\n"; } else { std::cout << "The word is not a palindrome.\n"; } return 0;}
Discover more from Tech For Talk
Subscribe to get the latest posts sent to your email.
Leave a Reply