[comp.sources.misc] Program to enumerate all words derivable from a telephone number

bobk@mntgfx.MENTOR.COM (Bob Kelley) (08/05/87)

/*
 * enumerate all words derivable from a telephone number - phony.c
 *
 * Usage: phony <telephone number less than 32 characters>
 *
 * Non-digits, '0', and '1' in the phone number are transcripted as is.
 * The digits '2' through '9' are cycled through their letter equivalents.
 *
 * I don't care what you do with this program, which was inspired by a
 * telephone number which I found impossible to remember.  Robert J. Kelley 8/3/87
 */

#include <stdio.h>

char *digits[] = { "abc",
                   "def",
                   "ghi",
                   "jkl",
                   "mno",
                   "prs",
                   "tuv",
                   "wxy" };

char    buf[32];

enumerate (s, b)
        char    *s;
        char    *b;
{
        char    *p;

        if (b >= buf + sizeof(buf)) {
                printf ("phony: Telephone number longer than 32 characters.\n");
                exit (1);
        }

        if (*s == 0)
                printf ("%s\n", buf);
        else if ('2' <= *s && *s <= '9') {
                for (p = digits[*s - '2']; *p; ++p) {
                        *b = *p;
                        enumerate (s + 1, b + 1);
                }
       } else {
                *b = *s;
                enumerate (s + 1, b + 1);
        }
}

main (argc, argv)
        int     argc;
        char    **argv;
{
        char    *s;

        if (argc != 2) {
                printf ("usage: phony <phone number>\n");
                exit (1);
        }

        enumerate (argv[1], buf);
}