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

C++ 带堆栈和队列的字符串回文(C+;+;),c++,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:" <<

这可以很好地编译,并且在没有空格的情况下运行良好,但是一旦我在其中放入空格,它要么告诉我它不是回文,要么超时。任何帮助都将不胜感激

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;
}
int main()
{
队列q;
堆栈s;
把线串起来;
整数不匹配=0;
割断绳子;
int i=0;
而(cin.peek()!='\n')
{
cin>>_字符串[i];
if(isalpha(_字符串[i]))
{
q、 推(推)上(弦[i]);
s、 推(推)上(弦[i]);
}
i++;
}
而((!q.empty())&&(!s.empty())
{
如果(q.front()!=s.top())
++错配;
q、 pop();
s、 pop();
}
如果(不匹配==0)
为什么这么复杂

你可以简单地做:

#include <string>
#include <algorithm>

bool is_palindrome(std::string const& s)
{
  return std::equal(s.begin(), s.begin()+s.length()/2, s.rbegin());
}
#包括
#包括
bool是回文(std::string const&s)
{
返回std::equal(s.begin(),s.begin()+s.length()/2,s.rbegin());
}
首先是线路

cin >> the_string;
没有完整的行。请使用此行

getline(cin, the_string);
第二,在调试算法时,打印出大量信息。例如,如果添加行

cout << "You entered: '" << the_string << "'" << endl;

cout我得到了这个解决方案,效果很好

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;
    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;
}
int main()
{
队列q;
堆栈s;
把线串起来;
整数不匹配=0;
这根绳子[我];
if(isalpha(_字符串[i]))
{
q、 推(推)上(弦[i]);
s、 推(推)上(弦[i]);
}
i++;
}
而((!q.empty())&&(!s.empty())
{
如果(q.front()!=s.top())
++错配;
q、 pop();
s、 pop();
}
如果(不匹配==0)
cout
void main()
{
队列q;
堆栈s;
字符字母;
整数不匹配=0;
信纸;
q、 推(信);
s、 推(信);
int i=0;
而(cin.peek()!='\n')
{
信;
如果(伊萨尔法(信))
{
q、 推(信);
s、 推(信);
}
i++;
}
而((!q.empty())&&(!s.empty())
{
如果(q.front()!=s.top())
++错配;
q、 pop();
s、 pop();
}
如果(不匹配==0)
{

cout
cin>>字符串不能像提示那样读取一行。为什么这样复杂的解决方案?你可以在没有堆栈或队列的情况下进行回文检查。你也可以在没有任何额外空间要求的情况下进行回文检查。@user93353我想OP正在做他的作业,使用堆栈和队列是一项要求。是的,作业是最重要的原因,谢谢各位!虽然我同意这是一个很好的方法,但您确实需要去掉空格,以等效于他的算法所尝试的内容。这更适合作为问题的注释(并且已经作为注释存在)是的,正如Burhan所说,这是一个家庭作业问题;)感谢各位的输入,非常感谢!他在两个地方阅读。在使用
cin>>字符串进行循环之前;
他先阅读用户类型的作品,然后在循环中使用
cin>>字符串[i]
他一个字符一个字符地读。请注意,如果用户输入“str1 str2_longer__than_str1”,它将溢出_字符串,因为它将保持“str2_longer_than_str1”,而其大小只适合“str1”。谢谢,fbafelipe是对的,我删除了第一个cin>>这个_字符串;并使用了getline(cin,这个_字符串);而且效果很好,谢谢你们!请解释一下你们改变了什么以及为什么
void main()
{
    queue<char> q;
    stack<char> s;
    char letter;
    int mismatches = 0;


    cout << "Enter a word and I will see if it's a palindrome:" << endl;
    cin >> letter;
    q.push(letter);
    s.push(letter);

    int i = 0;
    while (cin.peek() != '\n')
    {
        cin >> letter;
        if (isalpha(letter))
        {
            q.push(letter);
            s.push(letter);
        }
        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;
    }
    cout << endl;
    cout << "Homework done!" << endl;
    cout << "You are Welcome!" << endl;
    system("pause");

}