qc.hlp (Table of Contents; Topic list)
TOKEN.C
                                             Up Contents Index Back
────────────────────────────────────────────────────────────────────────────
 
/* TOKEN.C illustrates tokenizing and searching for any of several
 * characters. Functions illustrated include:
 *      strcspn         strspn          strpbrk         strtok
 */
 
#include <string.h>
#include <stdio.h>
 
void main()
{
    char string[100], vowels[] = "aeiouAEIOU", seps[] = " \t\n,";
    char *p;
    int  count;
 
    printf( "Enter a string: " );
    gets( string );
 
    /* Delete one word at a time. */
    p = string;
    while( *p )
    {
        printf( "String remaining: %s\n", p );
        p += strcspn( p, seps );    /* Find next separator     */
        p += strspn( p, seps );     /* Find next nonseparator  */
    }
 
    /* Count vowels. */
    p = string;
    count = 0;
    do
    {
        p = strpbrk( p, vowels );   /* Find next vowel         */
        count++;
    } while( *(p++) );
    printf( "\nVowels in string: %d\n\n", count - 1 );
 
    /* Break into tokens. */
    count = 0;
    p = strtok( string, seps );     /* Find first token        */
    while( p != NULL )
    {
        printf( "Token %d: %s\n", ++count, p );
        p = strtok( NULL, seps );   /* Find next token         */
    }
}