您的位置:首页 > 其它

判断字符串是否为回文

2014-03-10 22:38 239 查看
好久没练习写C语言了,最近要慢慢把C的感觉找回来,决定每天一练。今天的练习题目是:判断一个字符串是否为回文。

#include <stdio.h>
#include <string.h>
/*
Return 1 if success,otherwise 0
*/
int IsPalindrome(int Left, int Right, char *str, int Length)
{
if(str == NULL || Length == 0 || Length == 1)
return 1;
if((str[Left] != str[Right]) && Left <= Right)
{
return 0;
}
return IsPalindrome(Left+1, Right-1,str, Length-2);
}
int main(int argc,char *argv[])
{
char *str;
int Left = 0;
int Length;
int Right;
if (argc != 2)
{
printf("Param is not less than 2!\n");
printf("%s [str]\n",argv[0]);
return -1;
}
str = argv[1];
Right = strlen(str)-1;
Length = Right + 1;
if ( IsPalindrome(Left, Right, str, Length) )
{
printf("String [%s] is Palindrome!\n", str);
}
else
{
printf("String [%s] is not Palindrome\n",str);
}
return 0;
}
测试结果:



本文出自 “果子” 博客,请务必保留此出处http://chenqin.blog.51cto.com/2356777/1372075
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: