#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct LLS {

 char *s;
 struct LLS *next;
} LLS;

int main (int argc, char **argv);
void Process (FILE *out, char *line);
int IsPalindrome (char *s);

int main (int argc, char **argv) {

  int r;
  FILE *in, *out;
  char line[81];

 in = fopen ("prob5.in","r");
 out = fopen ("prob5.out","w");
 for (;;) {
  fscanf (in,"%s",line);
 if (feof (in)) break;
  Process (out,line);
 }
 fclose (in);
 fclose (out);
 r = EXIT_SUCCESS;
 return r;
}

void Process (FILE *out, char *line) {

  char *c;
  int ii, found, ct, i, j;
  LLS *t, *l;

 ct = 0;
 l = NULL;
 i = 0;
 for (;;) {
 if (line[i]==0) break;
  j = i;
  for (;;) {
  if (line[j]==0) break;
   c = malloc ((j-i+2)*sizeof (char));
   ii = i;
   for (;;) { /* Build appropriate substring */
   if (ii > j) break;
    c[ii-i] = line[ii];
    ii++;
   }
   c[ii-i] = 0;
   if (IsPalindrome (c)) {
    found = 0;
    t = l;
    for (;;) { /* Search through linked list for string */
    if (found || t==NULL) break;
     found = strcmp (c,t->s)==0;
     t = t->next;
    }
    if (!found) { /* If it's not there, add it */
     t = malloc (sizeof (LLS));
     t->s = c;
     t->next = l;
     l = t;
     ct++;
    } else { /* If it is there, deallocate */
     free (c);
    }
   } else {
    free (c);
   }
   j++;
  }
  i++;
 }
 fprintf (out,"The string \"%s\" contains %d palindromes.\n",line,ct);
 for (;;) { /* Release memory */
 if (l==NULL) break;
  t = l;
  l = t->next;
  free (t->s);
  free (t);
 }
}

int IsPalindrome (char *s) {

  int i, good;

 i = 0;
 good = 1;
 for (;;) {
 if (!good || i==strlen(s)/2) break;
  good = s[i]==s[strlen(s)-i-1];
  i++;
 }
 return good;
}
