Search code examples
c++stringqueuestackpalindrome

Stack and Queue Palindrome Program


I am writing a program to determine if a user-entered string of characters is a palindrome. The program compiles, however when output is printed, all strings of characters are determined to be palindromes even if they are not. I read my textbook varies times, reviewed and debugged the code dozens of times, looked at other similar palindrome questions, but I am still lost.

My code is as follows:

 #include <iostream>
    #include <stack>
    #include <queue>
    #include <string>
    using namespace std;

    int main (void)
    {
      stack <char> s;
      queue <char> q;
      string letter;
      int length;

      cout<<"Please enter a series of characters."<<endl;
      getline (cin, letter);
      length = letter.size();

      for (int i=0; i<length; i++)
        {
          q.push(i);
          s.push(i);
        }

         bool isPalindrome = true;
         while (isPalindrome==true && (!s.empty() && !q.empty()))
         {
           if (s.top() != q.front())
            {
              isPalindrome = false;
            }
          else
           {
            q.pop();
            s.pop();
           }
         }

       if(isPalindrome==false && (s.empty() && q.empty()))
         {
          cout<<"True or false: "<<isPalindrome<<endl;
          cout<<"Is not a palindrome."<<endl;
         }
       else
         {
           cout<<"Is a palindrome."<<endl;
         }

    }

I would greatly appreciate if someone could explain why this is happening. Thank you!


Solution

  • Well, for starters, you push integers instead of characters. To push the characters in the string use q.push(letters[i]) instead of q.push(i).

    You can also get rid of the stack and push half of the string to the queue and then compare it to the other half. Like this:

    #include <iostream>
    #include <queue>
    #include <string>
    
    using namespace std;
    
    int main (void)
    {
      queue <char> q;
      string letter;
      int length;
    
      cout<<"Please enter a series of characters."<<endl;
      getline (cin, letter);
    
      bool isPalindrome = false;
    
      if (letters.size() > 0)
      {
        int length = letter.size() / 2;
    
        for (int i=0; i<length; i++)
        {
          q.push(letters[i]);
        }
    
        isPalindrome = true;
    
        for (int i = 1; i <= length && isPalindrome; ++i)
        {
          isPalindrome = q.front() == letters[letters.size() - i];
          q.pop();
        }
      }
    
      if(!isPalindrome)
      {
        cout<<"Is not a palindrome."<<endl;
      }
      else
      {
        cout<<"Is a palindrome."<<endl;
      }
    
      return 0;
    }
    

    Or you can actually just avoid heavy data structures for such a simple task and use a simple loop to do this:

    bool isPalindrome = false;
    int len = letters.size();
    
    if (len > 0)
    {
      isPalindrome = true;
    
      for (int i = 0; i < len / 2 && isPalindrome; ++i)
      {
        isPalindrome = letters[i] == letters[len - i - 1];
      }
    }