home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / utils / phonegen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  1.4 KB  |  61 lines

  1. 18-Dec-85 20:39:55-MST,1483;000000000001
  2. Return-Path: <unix-sources-request@BRL.ARPA>
  3. Received: from BRL-TGR.ARPA by SIMTEL20.ARPA with TCP; Wed 18 Dec 85 20:39:50-MST
  4. Received: from usenet by TGR.BRL.ARPA id a005480; 18 Dec 85 21:51 EST
  5. From: Liudvikas Bukys <bukys@rochester.uucp>
  6. Newsgroups: net.sources
  7. Subject: phone_gen -- a simple efficient general phone number letterizer
  8. Message-ID: <14090@rochester.UUCP>
  9. Date: 18 Dec 85 17:36:54 GMT
  10. Keywords: telephone number permutation
  11. To:       unix-sources@BRL-TGR.ARPA
  12.  
  13. Here is a little program which does just what the recently-posted
  14. "telno" program does, except that it is more general (no restrictions
  15. on phone number format, no compiled-in array sizes), more efficient
  16. (no big arrays, no calls to qsort()), and a lot simpler.
  17.  
  18. -------
  19.  
  20. /*
  21.  * generate all alphabetic strings corresponding to a phone number
  22.  *
  23.  * n.b.: assumes I can modify argv[][] in place!
  24.  */
  25.  
  26. int main(argc, argv)
  27. int argc;
  28. char **argv;
  29.     {
  30.     for (;  --argc > 0;  printf("\n"))
  31.         phone_gen(*++argv, 0);
  32.  
  33.     return (0);
  34.     }
  35.  
  36. char *table[] = { "abc", "def", "ghi", "jkl", "mno", "prs", "tuv", "wxy" };
  37.  
  38. phone_gen(s, i)
  39. char *s;
  40. int i;
  41.     {
  42.     char c, *s2;
  43.     
  44.     c = s[i];
  45.  
  46.     if (c == '\0')
  47.         printf("%s\n", s);
  48.     else if ('2' <= c && c <= '9')
  49.         for (s2= table[c-'2'];  s[i]= *s2++;)
  50.             phone_gen(s, i+1);
  51.     else
  52.         phone_gen(s, i+1);
  53.  
  54.     s[i] = c;
  55.     }
  56.  
  57. -------
  58.  
  59. I guess it also assumes that the characters '2' through '9' are
  60. contiguous in your character set.
  61.