home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
-
- main()
- {
- char str[] = "2*(X+Y)/(X+Z) - (X+10)/(Y-5)";
- char strCopy[41];
- char* tkn[3] = { "+-*/ ()", "( )", "+-*/ " };
- char* ptr;
-
- strcpy(strCopy, str); // copy str into strCopy
- printf("%s\n", str);
- printf("Using token string %s\n", tkn[0]);
- // the first call
- ptr = strtok(str, tkn[0]);
- printf("String is broken into: %s",ptr);
- while (ptr) {
- printf(" ,%s", ptr);
- // must make first argument a NULL character
- ptr = strtok(NULL, tkn[0]);
- }
-
- strcpy(str, strCopy); // restore str
- printf("\nUsing token string %s\n", tkn[1]);
- // the first call
- ptr = strtok(str, tkn[1]);
- printf("String is broken into: %s",ptr);
- while (ptr) {
- printf(" ,%s", ptr);
- // must make first argument a NULL character
- ptr = strtok(NULL, tkn[1]);
- }
-
- strcpy(str, strCopy); // restore str
- printf("\nUsing token string %s\n", tkn[2]);
- // the first call
- ptr = strtok(str, tkn[2]);
- printf("String is broken into: %s",ptr);
- while (ptr) {
- printf(" ,%s", ptr);
- // must make first argument a NULL character
- ptr = strtok(NULL, tkn[2]);
- }
- printf("\n\n");
- return 0;
- }
-