home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / learn / token.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-03  |  1.2 KB  |  48 lines

  1. /* TOKEN.C illustrates tokenizing and searching for any of several
  2.  * characters. Functions illustrated include:
  3.  *      strcspn         strspn          strpbrk         strtok
  4.  */
  5.  
  6. #include <string.h>
  7. #include <stdio.h>
  8.  
  9. main()
  10. {
  11.     char string[100];
  12.     static char vowels[] = "aeiouAEIOU", seps[] = " \t\n,";
  13.     char *p;
  14.     int  count;
  15.  
  16.     printf( "Enter a string: " );
  17.     gets( string );
  18.  
  19.     /* Delete one word at a time. */
  20.     p = string;
  21.     while( *p )
  22.     {
  23.         printf( "String remaining: %s\n", p );
  24.         p += strcspn( p, seps );    /* Find next separator     */
  25.         p += strspn( p, seps );     /* Find next non-separator */
  26.     }
  27.  
  28.     /* Count vowels. */
  29.     p = string;
  30.     count = 0;
  31.     while( *(p - 1) )
  32.     {
  33.         p = strpbrk( p, vowels );   /* Find next vowel         */
  34.         p++;
  35.         count++;
  36.     }
  37.     printf( "\nVowels in string: %d\n\n", count - 1 );
  38.  
  39.     /* Break into tokens. */
  40.     count = 0;
  41.     p = strtok( string, seps );     /* Find first token        */
  42.     while( p != NULL )
  43.     {
  44.         printf( "Token %d: %s\n", ++count, p );
  45.         p = strtok( NULL, seps );   /* Find next token         */
  46.     }
  47. }
  48.