home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap09 / acme.c next >
Encoding:
C/C++ Source or Header  |  1988-04-06  |  2.3 KB  |  88 lines

  1. /* acme.c  --     illustrate an assortment of the     */
  2. /*                C library string-handling routines  */
  3.  
  4. #include <stdio.h>         /* for NULL */
  5. #include <string.h>        /* for strchr(), et al */
  6.  
  7. #define NAME_PATTERN \
  8. "first<space>last  or\n\
  9. first<space>middle<space>last"
  10.  
  11. #define ADDRESS_PATTERN \
  12. "number<space>street<comma><space>city<comma>"
  13.  
  14. char Buf[BUFSIZ];        /* global I/O buffer */
  15.  
  16. main()
  17. {
  18.     char *ocp, *cp, *first, *last, *street, *city;
  19.     void Prompt(), Cant();
  20.  
  21.     printf("Acme Employment Questionaire\n");
  22.  
  23.     /*
  24.      * Expect first<space>last or
  25.      *        first<space>middle<space>last
  26.      */
  27.     Prompt("Full Name");
  28.      /* search forward for a space */
  29.     if ((cp = strchr(Buf,' ')) == NULL)
  30.         Cant("First Name", NAME_PATTERN);
  31.     *cp = '\0';
  32.     first = strdup(Buf);
  33.     *cp = ' ';
  34.  
  35.      /* Search back from end for a space */
  36.     if ((cp = strrchr(Buf,' ')) == NULL)
  37.         Cant("Last Name", NAME_PATTERN);
  38.     last = strdup(++cp);
  39.  
  40.     /*
  41.      * Expect number<space>street<comma><space>city<comma>
  42.      */
  43.     Prompt("Full Address");
  44.      /* search forward for a comma */
  45.     if ((cp = strchr(Buf,',')) == NULL)
  46.         Cant("Street", ADDRESS_PATTERN);
  47.     *cp = '\0';
  48.     street = strdup(Buf);
  49.  
  50.      /* Search forward from last comma for next comma */
  51.     if ((ocp = strchr(++cp,',')) == NULL)
  52.         Cant("City", ADDRESS_PATTERN);
  53.     *ocp = '\0';
  54.     city = strdup(++cp);
  55.     
  56.     printf("\n\nYou Entered:\n");
  57.     printf("\tFirst Name: \"%s\"\n", first);
  58.     printf("\tLast Name:  \"%s\"\n", last);
  59.     printf("\tStreet:     \"%s\"\n", street);
  60.     printf("\tCity:       \"%s\"\n", city);
  61.  
  62. }
  63.  
  64. void Cant(char *what, char *pattern)
  65. {
  66.     printf("\n\n\bFormat Error!!!\n");
  67.     printf("Can't parse your %s.\n", what );
  68.     printf("Expected an entry of the form:\n\n");
  69.     printf("%s\n\nAborted\n", pattern);
  70.     exit(1);
  71. }
  72.  
  73. void Prompt(char *str)
  74. {
  75.     while (1)
  76.         {
  77.         printf("\n%s: ", str );
  78.         if (gets(Buf) == NULL || *Buf == '\0')
  79.             {
  80.             printf("Do you wish to quit? ");
  81.             if (gets(Buf) == NULL || *Buf == 'y')
  82.                 exit (0);
  83.             continue;
  84.             }
  85.         break;
  86.         }
  87. }
  88.