Interview Questions

How to find the longest palindrome is a string

Software QA/Tests Interview Questions from Microsoft


(Continued from previous question...)

How to find the longest palindrome is a string

Question:
How to find the longest palindrome is a string


maybe an answer:


#include&stdio.h>
#include&string.h>
int main()
{
int i,j,a[256],max=0,c=0,w,u,k,p, mc, l, st, e;
char s[30];
printf("enter the string\n");
gets(s);
mc = 1;
l = strlen(s);
c = 1; st = e = 0;
for(i = 0;i&l;i++)
{
j = i-1;k = i+1; c = 1;
while(1)
{ if(j>=0 && k<l && (s[j] == s[k]))
{
j--;
k++;
c+=2;
continue;
}
if(k<l && (s[i] == s[k]))
{
k++;
c+=1;
continue;
}
else if(mc<c)
{
if(c==2)
st = j;
else
st = j+1;
e = k-1;
mc = c;
}
break;
}

}
if(mc <=1)
printf("there isnt any palindrome");
else
{
printf("%d", mc);
for(;st<=e;st++)
printf("\t%c", s[st]);
}
getchar();
return 0;
}

(Continued on next question...)

Other Interview Questions