输入任意内容一行字符串s(长度小于5000),求最大回文字母串。不考虑大小写和非字母符号,原样输出,如果有多个字母串,则输出最左边的一个,例如:
输入:
CdasdU:3Madam,I'm Adam.
输出:
:3Madam,I'm Adam.
代码如下:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char s[5000];
int main()
{
int max,start,end,times,flag = 1;//结束标记
gets(s);
for (max = strlen(s); max > 0 && flag; max--)//回文最大长度
{
for (times = 0; times <= strlen(s) - max && flag; times++)//要比较子串的次数
{
//判断是否为回文数
for (start = times,end = times + max - 1; start < end; start++,end--)
{
if (!isalpha(s[start]))//如果不是字母
{
end++;
continue;
}
if (!isalpha(s[end]))//如果不是字母
{
start--;
continue;
}
//如果不相等
if (s[start]-s[end]!=32 && s[start]-s[end] != -32 && s[start]-s[end]!=0)
{
break;
}
}
if (start >= end)
{
flag = 0;//找到了标记0
end = times + max - 1;
while (times <= end)
{
printf("%c",s[times++]);
}
printf("\n");
}
}
}
return 0;
}