码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-03-05 14:29:03      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

判断字符串是否是回文:

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\n", palindrome(p1) ? "yes" : "no", p1);
    printf("%3s : %s\n", palindrome(p2) ? "yes" : "no", p2);

    return 0;
}

编译

g++ -o palindrome palindrome.cpp

运行

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

再见……

 

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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!