题目来自力扣:
我实现代码如下:
int judgmentString(char* s, int start, int sLenth)
{
char sValidAtion[1000] = "\0";
int count = 0;
for (int i = start; i < sLenth; i++)
{
sValidAtion[count++] = s[i];
}
count = start + sLenth - 1;
for (int i = start; i < sLenth / 2; i++)
{
if (sValidAtion[i] != sValidAtion[count--])
{
return 0;
}
else
{
return 1;
}
}
}
int main()
{
int record = 0;
int start = 0;
char* s = (char*)malloc(1000);
memset(s, 0, 1000);
printf("请用户输入字符串:");
scanf("%s", s);
while (getchar() != '\n');
int sLenth = strlen(s);
printf("字符串的长度为:%d\n", sLenth);
for (int i = 0; i < sLenth; i++)
{
for (int j = i + 1; j < sLenth; j++)
{
if (judgmentString(s, i, j))
{
if (record < j - i)
{
record = j - i;
start = i;
}
}
}
}
printf("最长的回文数是:");
if (record == 0)
{
printf("%c", s[0]);
return 0;
}
for (int i = start; i < start + record; i++)
{
printf("%c", s[i]);
}
return 0;
}