Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site rochester.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!panda!talcott!harvard!seismo!rochester!bukys From: bukys@rochester.UUCP (Liudvikas Bukys) Newsgroups: net.sources Subject: phone_gen -- a simple efficient general phone number letterizer Message-ID: <14090@rochester.UUCP> Date: Wed, 18-Dec-85 12:36:54 EST Article-I.D.: rocheste.14090 Posted: Wed Dec 18 12:36:54 1985 Date-Received: Fri, 20-Dec-85 03:06:46 EST References: <1206@panda.UUCP> Distribution: net Organization: U. of Rochester, CS Dept. Lines: 48 Keywords: telephone number permutation Here is a little program which does just what the recently-posted "telno" program does, except that it is more general (no restrictions on phone number format, no compiled-in array sizes), more efficient (no big arrays, no calls to qsort()), and a lot simpler. ------- /* * generate all alphabetic strings corresponding to a phone number * * n.b.: assumes I can modify argv[][] in place! */ int main(argc, argv) int argc; char **argv; { for (; --argc > 0; printf("\n")) phone_gen(*++argv, 0); return (0); } char *table[] = { "abc", "def", "ghi", "jkl", "mno", "prs", "tuv", "wxy" }; phone_gen(s, i) char *s; int i; { char c, *s2; c = s[i]; if (c == '\0') printf("%s\n", s); else if ('2' <= c && c <= '9') for (s2= table[c-'2']; s[i]= *s2++;) phone_gen(s, i+1); else phone_gen(s, i+1); s[i] = c; } ------- I guess it also assumes that the characters '2' through '9' are contiguous in your character set.