最大回文数问题

来源:互联网 发布:域名缩短器 编辑:程序博客网 时间:2024/06/06 19:32
输入任意内容一行字符串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;//找到了标记0end = times + max - 1;while (times <= end){printf("%c",s[times++]);}printf("\n");}}}return 0;}

原创粉丝点击