纪念逝去的岁月——C/C++字符串回文

判断字符串是否是回文:

1、

输入:hello world dlrow olleh

输出:1

2、

输入:nihao hello

输出:0

代码

#include <stdio.h>
#include <string.h>

int palindrome(char * p)
{
    if(NULL == p)
    {
        return 0;
    }
    int iLen = strlen(p);
    int iHalf = iLen / 2;
    int i = 0, iEnd = iLen - 1;
    for(i = 0; i <= iHalf; i++)
    {
        if(p[i] != p[iEnd - i])
        {
            return 0;
        }
    }

    return 1;
}

int main()
{
    char * p1 = "hello world dlrow olleh";
    char * p2 = "nihao hello";
    printf("%3s : %s
", palindrome(p1) ? "yes" : "no", p1);
    printf("%3s : %s
", palindrome(p2) ? "yes" : "no", p2);

    return 0;
}

编译

g++ -o palindrome palindrome.cpp

运行

$ ./palindrome 
yes : hello world dlrow olleh
 no : nihao hello

再见……

原文地址:https://www.cnblogs.com/fengbohello/p/4315575.html