小编jvi*_*021的帖子

栈和队列回文程序

我正在编写一个程序来确定用户输入的字符串是否是回文。程序可以编译,但是当打印输出时,所有字符串都被确定为回文,即使它们不是。我读了不同的教科书,检查和调试了代码数十次,查看了其他类似的回文问题,但我仍然迷失了。

我的代码如下:

 #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()))
         { …
Run Code Online (Sandbox Code Playgroud)

c++ string queue stack palindrome

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×1

palindrome ×1

queue ×1

stack ×1

string ×1