这是力扣上的题目,比较简单
代码
#include <stdio.h>
#include <string.h>
int main()
{
char s[100000]; //先设定一个字符串数组保存输入的字符串
char t[100000]; //再设定一个字符串数组保存回文字符串
printf("s = ");
scanf("%s",s);
int i,j,k,l;
int j1; //j1用来保存j的值(因为在后续的程序中j需要变化,但又不能破坏j的值)
l=strlen(s);
for(j=0;j<l-1;j++)
{
j1=j; //j1算是j的替身
for(k=l-1;k>0;k--)
{
if(s[j1]!=s[k]) //判断s[j1]同s[k]
{
j1=j;
memset(t,0,sizeof(t));
continue;
}
else
{
t[0]=s[j];
t[k-j]=s[k];
j1++;
}
}
if(strlen(t)) break;
}
if(l==1||l==2&&t[0]=='\0')
{
t[0]=s[0];
}
for(i=0;i<l&&t[i]!='\0';i++)
{
printf("%c",t[i]);
}
}