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

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

int main( )

{

queue q;

stack 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

#include

bool is_palindrome(std::string const& s)

{

return std::equal(s.begin(), s.begin()+s.length()/2, s.rbegin());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值