1.源码实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*判断是不是回文*/
short ispalin(char *str, int n)
{
int i = 0;
for(i=0; i+i<n; i++)
{
if(str[i] != str[n-1-i])
{
return 0;
}
}
return 1;
}
int maxsubpalin(char *a, char *b, int n)
{
int m = 0;
int u = 1;
int v = 0;
int i = 0;
int j = 0;
for(i=0; i<n; i++)
{
m = 1;
for(j=n-i; j>0; j--)
{
if(ispalin(a+i, j))
{
m = j;
if(m > u)
{
u = m;
v = i;
}
break;
}
}
}
strncpy(b, a+v, u);
b[u] = 0x00;
return u;
}
int main()
{
char str[1024];
char c[1024];
int n;
memset(str, 0x00, sizeof(str));
memset(c, 0x00, sizeof(c));
while(scanf("%s", str) != EOF)
{
n = strlen(str);
printf("%d\n", maxsubpalin(str, c, n));
memset(str, 0x00, sizeof(str));
memset(c, 0x00, sizeof(c));
}
return 0;
}
2.编译源码
$ gcc -o example examle.c -std=c89
4.运行及其结果
$ ./example
cabbade
4
网友评论