/* Problem 4--Double Near Palindromes
   This was straightforward as long as you remembered that even-length
   palindromes are never double near palindromes and odd-length
   palindromes always are. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *in, *out;

int main (int argc, char **argv);
int RevCount (char *s, int f, int l);

int main (int argc, char **argv) {

  char Word[30];
  int good, i;

 in = fopen ("prob4.in","r");
 out = fopen ("prob4.out","w");
 while (fscanf (in,"%s",Word),strcmp (Word,"*END*")!=0) {
  good = 0;
  for (i=1;i<strlen(Word); i++) { /* check each possibility */
   if (RevCount (Word,0,i-1) && RevCount (Word,i,strlen(Word)-1)) {
    good = 1;
    break;
   }
  }
  if (good)
   fprintf (out,"%s is a double near palindrome.\n",Word);
  else
   fprintf (out,"%s is not a double near palindrome.\n",Word);
 }
 fclose (in);
 fclose (out);
 return EXIT_SUCCESS;
}

/* RevCount determines whether the word found in s[f..l] is a near
   palindrome */
int RevCount (char *s, int f, int l) {

  int i, ct=0;

 for (i=f; i < f+(l-f+1)/2; i++)
  ct += s[i] != s[f+l-i]; /* count differences */
 return ct==1 || (ct==0 && (l-f)%2==0);
  /* exactly one difference or no differences and an odd length */
}
