判断字符串是否为回文

好久没练习写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;
}

测试结果:

wKiom1MdzumBLzsbAAGcdD62n9c283.jpg

你可能感兴趣的:(C语言,回文)