home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_12 / 1012077a < prev    next >
Text File  |  1992-10-13  |  689b  |  36 lines

  1. /* token3.c:    Read comma-delimited fields */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. main()
  7. {
  8.     char s[81];
  9.  
  10.     while (gets(s))
  11.     {
  12.         char *p, *sp = s;
  13.         int nchars;
  14.  
  15.         do
  16.         {
  17.             /* Make p point at next comma, or '\0'*/
  18.             if ((p = strchr(sp,',')) == NULL)
  19.                 p = sp + strlen(sp);
  20.             nchars = p - sp;
  21.  
  22.             /* Print the field */
  23.             if (sp > s)
  24.                 putchar(',');
  25.             printf("\"%.*s\"",nchars,sp);
  26.  
  27.             /* Position at start of next field */
  28.             sp = p+1;
  29.         } while (*p != '\0');
  30.  
  31.         putchar('\n');
  32.     }
  33.  
  34.     return 0;
  35. }
  36.