使用c++自带的栈完成对回文字符串(正反读都相同的字符序列)的判别

注:size_t 声明表示一种无符号整数,他是一种能够以字节为单位表示任何对象大小的类型,size_t 通常是 sizeof 运算符的返回类型,广泛用于标准库中以表示大小和计数

#include<iostream>
#include<string>
#include<vector>
#include<stack>
using std::vector;
using std::string;
using std::stack;
using std::cout;
using std::endl;
using std::cin;
int main()
{
    string str;
    getline(cin, str);
    size_t mid = str.size() / 2;
    stack<int> int_stack;
    for (size_t i = 0; i != mid; i++)
        int_stack.push(str[i]);
    size_t next = 0;
    if (str.size() % 2 == 0)    //if the number of str is even
        next = mid;
    else                        //if the number of str is odd
        next = mid + 1;
    for (size_t i = next; i != str.size(); i++)
    {
        if (str[i] != int_stack.top())
            break;
        int_stack.pop();
    }
    if (int_stack.empty())
        cout << "yes" << endl;
    else
        cout << "no" << endl;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值