/* AllPhoneWord.c - try to make words from a phone number.

                                Ron Charlton
                             9002 Balcor Circle
                             Knoxville, TN 37923
   
                             Phone: (615)694-0800
   
                              PLINK: R*CHARLTON
                           BINTNET: charltr@utkvx1

                                 05-Jul-90

This program is in the public domain.  It may be used for any purpose.

Algorithm:  Generate ALL of the letters in a phone number with a bunch of
nested 'for' loops.  From 1 to 7 digits may be entered.

history:
v1.1   changed to lowercase, added version number
*/

char version [] = "v1.1";

#include <stdio.h>
#include <ctype.h>

#define FALSE 0
#define TRUE (!FALSE)

#ifdef MCH_AMIGA
#define CSI 0x9b
#define CLS 0x0c
#endif

#define MAXDIGITS 7
#define LINESPERSCREEN 10

int i [ MAXDIGITS ];			/* loop counter */

int WordsThisLine, MaxPerLine;
int lines, c;

char letters [][4] = { "@@@", "@@@", "abc", "def", "ghi", "jkl", "mno",
		       "prs", "tuv", "wxy" };

char Dstr [ 255+1 ];			/* string from user */
char work [ MAXDIGITS+1 ];		/* build string to print here */

/*------------------------------------------------------------------------*/

main()
{
int dig_pos, length, good;

intro();

do
  {
  good = TRUE;
  printf ( "\tEnter 1 to 7 digits (no 1 or 0) <RETURN> to quit): " );
  gets ( Dstr );
  length = strlen ( Dstr );
  if ( length == 0 ) exit ();
  if ( length > MAXDIGITS )
    {
    printf ( "\t\tEnter no more than %d digits.\n\n", MAXDIGITS );
    continue;
    }
  /* check for bad characters ( only 2-9 allowed ) */
  for ( dig_pos = 0; dig_pos < length; dig_pos++ )
    if ( !isdigit ( Dstr [ dig_pos ] ) || Dstr [ dig_pos ] == '1' ||
		Dstr [ dig_pos ] == '0' )
      {
      printf ( "\t\tOnly digits 2-9 allowed.\n\n" );
      good = FALSE;
      break;
      }
  if ( good )
    {
    lines = 0;
    MaxPerLine = 40 / length;		/* how many "words" per line */
    WordsThisLine = 0;
    printf ( "\n\t\t" );
    generate ( 0, length );		/* generate all of the possibilies */
    printf ( "\n\n" );
    }
  } while ( length > 0 );
}

/*------------------------------------------------------------------------*/

intro()
{
#ifdef MCH_AMIGA
printf ( "%c", CLS );			/* clear screen */
printf ( "%c0;33;40m", CSI );		/* color 3,0 */
#endif
printf ( "\n\t\t\t    AllPhoneWord %s\n\n",version );
printf (   "\t\t\t           by\n\n" );
printf (   "\t\t\t      Ron Charlton\n" );
printf (   "\t\t\t   9002 Balcor Circle\n" );
printf (   "\t\t\t   Knoxville, TN 37923\n\n" );
#ifdef MCH_AMIGA
printf ( "%c0;31;40m", CSI );		/* color 1,0 */
#endif

printf ( "\tAllPhoneWord tries to create words out of telephone numbers\n" );
printf ( "\tthat you enter.  It will print out all of the possible\n" );
printf ( "\tcombinations of letters in the phone number.  Note that this\n" );
printf ( "\twill be 2,187 'words' for a 7-digit number (and a lot of time\n" );
printf ( "\tlisting them all). Digits 0 and 1 are not accepted (why?).\n\n" );

printf ( "\tIf your number has 0 or 1 in it you can enter the other\n" );
printf ( "\tdigits in several groups.  It is good to try different\n" );
printf ( "\tgroups of digits anyway, for example, if your number is\n" );
printf ( "\t234-5678, then try 234 & 5678; 2345 & 678; 23456 & 78;\n" );
printf ( "\t2345678, etc.\n\n" );
}

/*------------------------------------------------------------------------*/
 
generate ( pos, len )
  /* generate the "words" recursively (creates "len" nested "for" loops) */
  int pos, len;
  {
  int s;
  if ( pos < len )
    for ( i [ pos ] = 0; i [ pos ] < 3; ++i[pos] )
      {
      generate ( pos+1, len );
      }
  else
    {
    /* build string and print it */
    for ( s = 0; s < len; s++ )
      work [ s ] = letters [ Dstr[s] - '0'] [ i[s] ];
    work [ s ] = '\0';
    ++WordsThisLine;
    if ( WordsThisLine > MaxPerLine )		/* time for new line */
      {
      WordsThisLine = 1;
      ++lines;
      if ( lines > LINESPERSCREEN )
 	{
	lines = 0;
	printf ( "\n\t<RETURN> to continue...  (%s)", Dstr );
	c = getchar();
	}
      printf ( "\n\n\t\t" );
      }
    printf ( "%s ", work );
    }
  }
