回文字符串C语言判断函数

C语言判断字符串是否为回文

#include 
#include 
#include 

/* if str is palindrome return 0,or return 1*/
int ispalindrome(const char *str,int length)
{
    int i = 0;

    if(str[0] == '\0')
        return 1;

    for(i = 0; i < length/2; i++)
    {
        if(str[i] != str[length-i-1])
            break;
    }

    if(i == length/2)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int main(int argc, char *argv[])
{
    int ret = 0;
    if(argc != 2)
    {
        printf("the argument is not right\n");
        exit(1);
    }

    ret = ispalindrome(argv[1], strlen(argv[1]));

    if(0 == ret)
    {
        printf("%s is palindrome\n",argv[1]);
    }
    else
    {
        printf("%s is not palindrome\n",argv[1]);
    }

    return 0;

}

运行
gcc test_palindrome.c

./a.out afdfa
afdfa is palindrome

./a.out abd
abd is not palindrome

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