Listing 5 strtokf.c — recognizes tokens via a user-supplied function that identifies acceptable characters

/* strtokf.c: Collect tokens via a function */

#include <stdio.h>
#include <string.h>

static char *sp = NULL; /* Internal string position */

char*strtokf(char*s, int (*f)(char))
{
   if (s != NULL)
      sp= s;     /* Remember string address */
   if (sp == NULL)
      return NULL;  /* No string supplied */

   /* Skip leading, unwanted characters */
   while (*sp != '\0' && !f(*sp))
      ++sp;
   s = sp;             /* Token starts here */

   /* Build token */
   while (*sp != '\0' && f(*sp))
      ++sp;
   if (*sp != '\0')
      *sp++ = '\0'; /* Insert string terminator */

   return strlen(s) ? s : NULL;
}

/* End of File */