数据结构——算法之(006)(判断字符串是不是是对称的即(回文字符串))

数据结构——算法之(006)(判断字符串是否是对称的即(回文字符串))

【申明:本文仅限于自我归纳总结和相互交流,有纰漏还望各位指出。 联系邮箱:Mr_chenping@163.com】

题目:判断一字符串是不是对称的,如:abccba 123454321

题目分析:

一、通过2个指针start、end分别指向数组的头和尾,一旦*start != *end 立即返回即可

算法实现:

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

int check_str(char *str)
{
    char *s = str;
    char *e = str + strlen(str)-1;

    while(s < e)
    {
        if(*s != *e)
            return 0;
        s++;
        e--;
    }
    return 1;
}

int main()
{
    cout<<"out="<<check_str("1234321")<<endl;
    cout<<"out="<<check_str("125321")<<endl;
    cout<<"out="<<check_str("123321")<<endl;
    return 0;
}