求字符串是回文的最长子串


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int ishuiwen(char *p)
{
int i = strlen(p);
for(int j=0;j<=i/2;j++)
{
if(p[j] !=p[i-j-1])
{
return 0;
}
}
return 1;
}

char * getSubString(char *p, int m, int slen)
{
char *buf = new char[strlen(p) + 1];
int count = 0;
for(int k=m;p[k]!='\0'&& count<slen;k++)
{
buf[count++] = p[k];
}
buf[count] = '\0';
return buf;
}


int gerlongStr(char *p)
{
int len = strlen(p);
int i=0;
int flag = 0;
while(i<=len)
{
int slen = len-i;
for(int j=0;j<=i;j++)
{
char *buf = getSubString(p, j, slen);
//printf("%s\n", buf);

if(ishuiwen(buf)==1)
{
printf(buf);
printf("\n");
flag =1;
}

free(buf);
}
if(flag)
return flag;
i++;
}
return flag;
}

int main()
{
gerlongStr("helloworldlrowdfsg");
return 0;
}

posted @ 2020-08-17 22:14  牛牛码代码  阅读(66)  评论(0编辑  收藏  举报