/*
 * DICTCOMPRESS
 *
 * Summary:  Reads a dictionary (list of words, one per line) and
 * creates a compressed dictionary (still a list of words), according to
 * a clever little algorithm.
 *
 * Compression Algorithm:  For each word, count the number of letters
 * it had in common with the previous word.  When printing that word,
 * then instead of the common letters, print that number; exception is if
 * the number is 1 or 0.
 *
 * For example, given the list
 *             aardvark
 *             aardwolf
 *             aardwolves
 *             absent
 *             absentia
 *             ...,
 * the output would be
 *             aardvark
 *             4wolf
 *             7ves
 *             absent
 *             6ia
 *             ....
 *
 * This scheme works very well for large sorted word lists, and not
 * well at all for short or unsorted ones.
 *
 * Note:  I wrote the program, but I did not invent the algorithm.
 * I forget who did.  I found it somewhere, probably on The Net.
 *
 */

#include <stdio.h>

#define MAX_LENGTH 80

char* Myfgets (char*, int, FILE*);
int CountCommon (char*, char*);


main(int argc, char** argv)
{
   char word1[MAX_LENGTH+1];
   char word2[MAX_LENGTH+1];
   int common;

   if (Myfgets (word1, MAX_LENGTH+1, stdin) == NULL)
      exit (0);   /* empty input file */
   else
      printf ("%s\n", word1);

   while (Myfgets (word2, MAX_LENGTH+1, stdin) != NULL) {
      common = CountCommon (word1, word2);

      if (common <= 1)
         printf ("%s\n", word2);
      else
         printf ("%d%s\n", common, word2+common);

      strcpy (word1, word2);
      }

   exit(0);
}



/*
 * CountCommon -- Finds the length of the longest shared prefix of
 *                two words.
 */

int CountCommon (char* word1, char* word2)
{
   int i = 0;
   while ((i < strlen(word1)) && (i < strlen (word2))) {
      if (word1[i] != word2[i])
         return i;
      i++;
      }
   return i;
}



/*
 * Myfgets -- Calls fgets() and returns whatever fgets() would have
 *            returned, but with the special added bonus of stripping
 *            off the '\n' that fgets() so graciously includes.
 */

char* Myfgets (char* word, int length, FILE* fp)
{
   char* temp;
   temp = fgets (word, length, fp);
   if (temp != NULL)
      if (*(word+strlen(word)-1) == '\n')
         *(word+strlen(word)-1) = '\0';
   return temp;
}

