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

  1. /* strtokf.c:     Collect tokens via a function */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. static char *sp = NULL;    /* Internal string position */
  7.  
  8. char *strtokf(char *s, int (*f)(char))
  9. {
  10.     if (s != NULL)
  11.         sp = s;         /* Remember string address */
  12.     if (sp == NULL)
  13.         return NULL;    /* No string supplied */
  14.  
  15.     /* Skip leading, unwanted characters */
  16.     while (*sp != '\0' && !f(*sp))
  17.         ++sp;
  18.     s = sp;             /* Token starts here */
  19.  
  20.     /* Build token */
  21.     while (*sp != '\0' && f(*sp))
  22.         ++sp;
  23.     if (*sp != '\0')
  24.         *sp++ = '\0';   /* Insert string terminator */
  25.  
  26.     return strlen(s) ? s : NULL;
  27. }
  28.  
  29.