#include <stdio.h>
#include <stdlib.h>

/* 
   Sallows pangram generator
   Adam Sampson, azz@gnu.org

   This program is freely distributable under the terms of the GNU
   General Public License version 2 or later.
*/

#define USE_RANDOM_METHOD 1
#define SHOW_COUNTS 0

char *start = "adam sampson found this sentence, which includes  and ";

char *nums[] = { "no", "one", "two", "three", "four", "five", "six",
		"seven", "eight", "nine", "ten", "eleven", "twelve",
		"thirteen", "fourteen", "fifteen", "sixteen",
		"seventeen", "eighteen", "nineteen" };
char *tens[] = { "", "", "twenty", "thirty", "forty", "fifty",
		"sixty", "seventy", "eighty", "ninety" };

unsigned char counts[26], lastcounts[26], startcounts[26];

void countstring(char *s) {
  register char c;

  while(*s) {
    c=*s++;
    if (c>='a' && c<='z') counts[c-'a']++;
  } 
}

void printresult() {
  register int i, j;

  printf("%s ", start);
  for(i=0; i<26; i++) {
    if (lastcounts[i]<20) {
      printf("%s ", nums[lastcounts[i]]);
    } else {
      if (j=lastcounts[i]%10) {
	printf("%s-%s ", tens[lastcounts[i]/10], nums[j]);
      } else {
	printf("%s ", tens[lastcounts[i]/10]);
      }
    }
    printf("%c%s, ", 'A'+i, (lastcounts[i]==1)?"":"s");
  }
  printf("\n\n");
}

int main(int argc, char **argv) {
  register int i, j;

  if (argc > 1) start = argv[1];

  srand(time(NULL));
  for(i=0; i<26; i++) lastcounts[i]=rand()%30;

  /* count the initial string */
  memset(counts, 0, 26);
  countstring(start);
  memcpy(startcounts, counts, 26);

  while(1) {
    /* clear the counts to the initial string's values */
    memcpy(counts, startcounts, 26);

    for(i=0; i<26; i++) {
      /* count the letter */
      counts[i]++;
      /* if not one, count an s */
      if (lastcounts[i]!=1) counts['s'-'a']++;
      /* count the number */
      if (lastcounts[i]<20) {
	countstring(nums[lastcounts[i]]);
      } else {
	if (j = lastcounts[i]%10) countstring(nums[j]);
	countstring(tens[lastcounts[i]/10]);
      }
    }

#if SHOW_COUNTS
    putchar(13);
    for(i=0; i<26; i++) {
      printf("%3d", counts[i]);
    }
#endif

    /* if the string is true, exit */
    if (!memcmp(lastcounts, counts, 26)) {
      printresult();
      exit(0);
    }

#if USE_RANDOM_METHOD
    for(i=0; i<26; i++) {
      if (lastcounts[i]!=counts[i]) {
	if (lastcounts[i]<counts[i]) {
	  counts[i] -= rand()%(counts[i]-lastcounts[i]);
	} else {
	  counts[i] += rand()%(lastcounts[i]-counts[i]);
	}
      }
    }
#endif

    memcpy(lastcounts, counts, 26); 
  }
}




