题目来自力扣:

我实现代码如下:
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;
}
本文介绍了一种通过编程方式寻找给定字符串中最长回文子串的方法。使用C语言实现了一个程序,该程序能够接收用户输入的字符串,并找出其中最长的回文子串。通过双重循环遍历字符串,利用辅助函数判断子串是否为回文,从而找到符合条件的最长子串。
1020

被折叠的 条评论
为什么被折叠?



