一天一算法:回文判断

问题描述:

什么是回文?如,aha, adda,单ahah就不是回文,等等

如何判断一串字符串是回文呢?

 

这里的想法是:我们利用队列的方式,找到字符的中间的位置,将中间字符之前的全部入栈,然后全部出栈,与中间字符之后的字符进行比较,如果全部一样,那么就是回文。

代码:

#include<iostream>
#include <queue>
#include <string.h>
using namespace std;

int main()
{
    char str[] = "ahaha";
    char tmp[10] = {0};
    int middle = sizeof(str)/sizeof(char) / 2 -1 ;

    queue<char> str_queue;
    for (int i = middle - 1; i >= 0; i--) {
        str_queue.push(str[i]);
    }
    
    int i = 0;
    while (!str_queue.empty()) {
        tmp[i] = str_queue.front();
        str_queue.pop();
        i++;
    }
    tmp[i] = '\0';
    
    if (memcmp(str+3, tmp, 2) == 0 ) {
        cout << "Yes" << endl;
    } else {
        cout  << "No " << endl;
    }

}

 

转载于:https://www.cnblogs.com/457220157-FTD/p/4063549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值