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