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

  1. /* l2words.c  --  employ an array of pointers to   */
  2. /*                strings to bust a line of text   */
  3. /*                into its component words         */
  4.  
  5. #include <stdio.h>          /* for NULL and BUFSIZ */
  6.  
  7. main()
  8. {
  9.     char **Line2words();    /* declare function type */
  10.     char **list;            /* pointer to pointer    */
  11.     char buf[BUFSIZ];       /* buffer for input      */
  12.     int  count, i, quote_flag;
  13.  
  14.     printf("Enter a line of text and I will break\n");
  15.     printf("it up for you.\n");
  16.  
  17.     if (gets(buf) == NULL)
  18.         exit(1);
  19.     
  20.     list = Line2words(buf, &count);
  21.  
  22.     for (i = 0; i < count; i++)
  23.         {
  24.         quote_flag = 0;
  25.         printf("<");
  26.         if (list[i] != buf)
  27.             {
  28.             if( list[i][-1] == '"')    /* negative subscript */
  29.                 {
  30.                 ++quote_flag;
  31.                 printf("\"");
  32.                 }
  33.             }
  34.         printf("%s", list[i]);
  35.  
  36.         if (quote_flag)
  37.             printf("\"");
  38.  
  39.         printf(">\n");
  40.         }
  41. }
  42.  
  43. #define MAXW 64
  44.  
  45. char **Line2words(char *line, int  *count)
  46. {
  47.     static char *words[MAXW];
  48.     int  index;
  49.  
  50.     index = 0;        /* zero internal index */
  51.  
  52.     while (*line != '\0')
  53.         {
  54.         /* turn spaces and tabs into zeros */
  55.         if (*line == ' ' || *line == '\t')
  56.             {
  57.             *(line++) = '\0';
  58.             continue;
  59.             }
  60.         words[index] = line++;    /* found a word */
  61.  
  62.         /* is it quoted? */
  63.         if ( *(words[index]) == '"')
  64.             {
  65.             /* Yes, advance pointer to just past quote. */
  66.             ++words[index];
  67.  
  68.             /* find next quote. */
  69.             while (*line && *line != '"')
  70.                 {
  71.                 ++line;
  72.                 }
  73.  
  74.             /* and turn it into a '\0'. */
  75.             if (*line)
  76.                 *(line++) = '\0';
  77.             }
  78.         else
  79.             {
  80.             /* otherwise skip to next space */
  81.             while (*line && *line != ' ' && *line != '\t')
  82.                 {
  83.                 ++line;
  84.                 }
  85.             }
  86.         if (++index == MAXW)
  87.             break;
  88.         }
  89.     *count = index;        /* set count via pointer   */
  90.     return (words);        /* return address of array */
  91. }
  92.