【发布时间】:2015-06-12 14:13:16
【问题描述】:
我正在编写一个程序来确定用户输入的字符串是否是回文。程序可以编译,但是当输出输出时,所有字符串都被确定为回文,即使它们不是。我多次阅读教科书,检查和调试代码数十次,查看其他类似的回文问题,但我仍然迷茫。
我的代码如下:
#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;
}
}
如果有人能解释为什么会发生这种情况,我将不胜感激。谢谢!
【问题讨论】:
-
你不应该按“letter[i]”而不是“i”吗?
标签: c++ string queue stack palindrome