Palindromes with stacks I'm creating a simple function to verify whether or not a string is a palindrome, and must use stacks in solving it.

bool isPalindrome(string word) {    
    // #1 determine length
    int n = word.size();

    // #3 make sure we have an even string
    if (n % 2 != 0)
        word.erase(0, 1);

    // #2 load the stack
    stack&lt;char&gt; s;
    for (int i = 0; i &lt; n / 2; i++) {
        s.push(word[i]);
        word.erase(i, 1);
    }

    // #4 if our stack is empty, then return true
    if (s.empty())
        return true;

    // #5 compare the first &amp; last letters for a valid palindrome
    /*if (s.top() == word[0]) {
        s.pop();
        word.erase(0, 1);
    } else
        return false;*/

    while (n != 0) {
        if (s.top() == word[0]) {
            s.pop();
            word.erase(0, 1);

            // return true only when our stack is empty
            if (s.empty())
                return true;
        } else
            return false;
    }
}


Is there a better way to do this? Are there any evident issues with the current state of the program?
First, a palindrome can have an odd number of characters:


  Even-character palindrome: 1221
  Odd-character palindrome: 13531


You only support even-character palindromes, which is bad, but even worse, you do not return an error about invalid input for an odd-digit palindrome, you remove the first digit.

Second, to help prevent errors, you should always use braces around one-liner if and loop statements:

if (n % 2 != 0) {
    word.erase(0, 1);
}


Third, don't leave dead code in there.  You have a chunk of code commented out, so that should be removed.

Fourth, your comments are in the order of #1, #3, #2, ...  Either the comments should be fixed or the code should be re-ordered.

Fifth, there is a mistake right here:

for (int i = 0; i &lt; n / 2; i++) {
    s.push(word[i]);
    word.erase(i, 1);
}


Sixth, it appears that you are using namespace std; somewhere in your code as you did not prefix stack or string.  This is not a good idea because namespaces can contain functions with the same name, so you can have problems with your code when you start using multiple namespaces, or even if you have a function in the same file that has the same name as a function in the namespace.  You can read more about it here.

So, with the word "qweewq", you first remove "q" so the word is "weewq".  Next, i == 1, so you remove "e", so the word is "wewq", and so on.  You should change this to:

for (int i = 0; i &lt; n / 2; i++) {
    s.push(word[0]);
    word.erase(0, 1);
}


This is a working solution:

bool isPalindrome(std::string word) {

    int wordSize = word.size();

    // #1 load the stack
    std::stack&lt;char&gt; s;
    for (int i = 0; i &lt; wordSize / 2; i++) {
        s.push(word[0]);
        word.erase(0, 1);
    }

    // #2 make sure word size is same as stack size (handle odd-character palindromes)
    if (word.size() &gt; s.size()) {
        word.erase(0, 1);
    }

    // #3 if our stack is empty, then return true
    if (s.size() == 0) {
        return true;
    }

    // #4 validate palindrome
    while (s.size() &gt; 0) {

        if (s.top() == word[0]) {
            s.pop();
            word.erase(0, 1);

            // return true only when our stack is empty
            if (s.size() == 0 || word.size() == 0) {
                return true;
            }
        }
        else {
            return false;
        }
    }
}


Improvements on this solution are given here.