home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / string5.cpp < prev    next >
C/C++ Source or Header  |  1993-03-30  |  1KB  |  47 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. main()
  5. {
  6.    char str[] = "2*(X+Y)/(X+Z) - (X+10)/(Y-5)";
  7.    char strCopy[41];
  8.    char* tkn[3] = { "+-*/ ()", "( )", "+-*/ " };
  9.    char* ptr;
  10.  
  11.    strcpy(strCopy, str); // copy str into strCopy
  12.    printf("%s\n", str); 
  13.    printf("Using token string %s\n", tkn[0]);
  14.    // the first call 
  15.    ptr = strtok(str, tkn[0]);
  16.    printf("String is broken into: %s",ptr);
  17.    while (ptr) {
  18.      printf("  ,%s", ptr);
  19.      // must make first argument a NULL character
  20.      ptr = strtok(NULL, tkn[0]);
  21.    } 
  22.  
  23.    strcpy(str, strCopy); // restore str
  24.    printf("\nUsing token string %s\n", tkn[1]);
  25.    // the first call 
  26.    ptr = strtok(str, tkn[1]);
  27.    printf("String is broken into: %s",ptr);
  28.    while (ptr) {
  29.      printf("  ,%s", ptr);
  30.      // must make first argument a NULL character
  31.      ptr = strtok(NULL, tkn[1]);
  32.    } 
  33.  
  34.    strcpy(str, strCopy); // restore str
  35.    printf("\nUsing token string %s\n", tkn[2]);
  36.    // the first call 
  37.    ptr = strtok(str, tkn[2]);
  38.    printf("String is broken into: %s",ptr);
  39.    while (ptr) {
  40.      printf("  ,%s", ptr);
  41.      // must make first argument a NULL character
  42.      ptr = strtok(NULL, tkn[2]);
  43.    } 
  44.    printf("\n\n");
  45.    return 0;
  46. }
  47.