The first thing to do is to indent the code so that you can read it . . .
Code:
#include <stdio.h>
#include <string.h>
#include <memory.h>
//#include <assert.h>
#define MAX_LEN 80
char szInput[MAX_LEN+1];
unsigned char B[(MAX_LEN+1)*MAX_LEN/8];
unsigned char Bit8[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
#define GETB(i,j) (B[(i)*10+((j)>>3)] & Bit8[(j) & 0x7])
#define SETB(i,j) (B[(i)*10+((j)>>3)] |= Bit8[(j) & 0x7])
int length;
int getcode()
{
int i,j,notfound1,notfound2=0;
if(length <2)
return 1;
memset(B,0,(length+1)*10); //MAX_LEN/8?
for(i=0;i <2;++i)
{
for(j=0;j <length;++j)
SETB(i,j); //B[j]=1;
}
for(i=2;i <=length;++i)
{
for(notfound1=1,j=0;j <=length-i;++j)
{
if(GETB(i-2,j+1)/*B[i-2][j+1]*/ && szInput[j]==szInput[j+i-1])
{
SETB(i,j); //B[j]=1;
//printf("%d,%d = 1\n",i,j);
notfound1=0;
}
}
if(notfound1)
{
if(notfound2)
return i-2;
notfound2=1;
}
else
{
notfound2=0;
}
}
if(notfound2)
return length-1;
return length;
}
int main(void)
{
while(scanf("%s",szInput)!=EOF)
{
length=strlen(szInput);
//assert(length <=MAX_LEN);
printf("%d\n",getcode());
}
return 0;
}
Of course, "legible" is a relative term.
I can't make much sense of that code, but I'd guess that the outer for loop steps through each character in the string, and the inner for loop determines how long of a palindrome is present at that point.
FWIW, here's my version:
Code:
#include <stdio.h>
#include <stddef.h>
#include <string.h>
int longest_palindrome(const char *string);
int main() {
char string[BUFSIZ], *p;
int length;
while(fgets(string, sizeof string, stdin)) {
/* remove the newline, if any, from the string */
if((p = strchr(string, '\n'))) *p = 0;
length = longest_palindrome(string);
printf("longest palindrome: %d\n", length);
}
return 0;
}
int longest_palindrome(const char *string) {
int length = (int)strlen(string), pal_length;
int start, end, offset;
int longest = 0;
int still_palindrome = 0;
if(!length) return 0;
for(start = 0; start < length; start ++) {
for(end = length - 1; end >= start; end --) {
pal_length = end - start + 1;
/* check if the string between string[start] and string[end] is a
palindrome. */
still_palindrome = 1;
for(offset = 0; still_palindrome && offset <= pal_length / 2;
offset ++) {
if(string[start + offset] != string[end - offset]) {
still_palindrome = 0;
}
}
if(still_palindrome) {
printf(" found: \"%.*s\"\n", pal_length, string + start);
if(pal_length > longest) {
printf(" (set as longest)\n");
longest = pal_length;
}
}
}
}
return longest;
}