#include <stdio.h>
char *longPlaindrome(char *str)
{
char *pointpal;
while (*str)
{
int i = 1;
if (*str == *(str + i) || *(str - i) == *(str + i))
{
if (*(str - i) == *(str + i))
pointpal = str - i;
else
pointpal = str;
i++;
while (*(str - (i - 1)) == *(str + i) || (*str - i) == *(str + i))
{
pointpal--;
i++;
}
}
str++;
}
return pointpal;
}
int main()
{
char *str1 = "jskhahad";
char *longestpal = longPlaindrome(str1);
char *point = longestpal;
int loop = 1, count = 0;
while (loop)
{
++count;
printf("%c", *longestpal);
longestpal++;
if (point == longestpal)
loop = 0;
if (*point == *longestpal)
point = longestpal + 1;
}
printf(" %d", count);
return 0;
}
有些复杂,但是可行。输出:啊哈3