带有堆栈和队列的字符串回文(C)

编译很好,没有空格,但是一旦我在其中放置空格要么告诉我它不是回文或超时.任何帮助将不胜感激!

int main( )
{
   queue<char> q;
   stack<char> s;
   string the_string;
   int mismatches = 0;
   cout << "Enter a line and I will see if it's a palindrome:" << endl;
   cin  >> the_string;

   int i = 0;
   while (cin.peek() != '\n')
   {
       cin >> the_string[i];
       if (isalpha(the_string[i]))
       {
          q.push(toupper(the_string[i]));
          s.push(toupper(the_string[i]));
       }
       i++;
   }

   while ((!q.empty()) && (!s.empty()))
   {
      if (q.front() != s.top())
          ++mismatches;

        q.pop();
        s.pop();
   }

   if (mismatches == 0)
       cout << "This is a palindrome" << endl;
   else
       cout << "This is not a palindrome" << endl;

   system("pause");
   return EXIT_SUCCESS;
}

最佳答案 为什么这么复杂?

你可以这样做:

#include <string>
#include <algorithm>

bool is_palindrome(std::string const& s)
{
  return std::equal(s.begin(), s.begin()+s.length()/2, s.rbegin());
}
点赞