欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c语言回文字判断

程序员文章站 2022-05-23 09:26:30
...

递归实现回文字判断

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include <unistd.h>
/*****tonytrek*****2021.5.7*****/
/*****回文字判断*****/
int back_foward(int,int,char*,int);
int main()
{
    char* str=malloc(10);
    scanf("%s",str);
    printf("string is %s\n",str);
    sleep(2);
    int front=0;
    int back=strlen(str)-1;
    back_foward(front,back,str,strlen(str));
    return 0;
}
int back_foward(int front,int back,char* str,int rest)
{
    printf("start\n");
    if(rest==0||rest==1)
    {
        printf("yes!\n");
        return 1;
    }
    else
    {
        if(str[front]==str[back])
        {
            front=front++;
            back=back--;
            rest=rest-2;
            back_foward(front,back,str,rest);  //a Recursion
        }
        else
        {
            printf("no...\n");
            return 0;
        }
    }
}