Strtok () delimiter string Function
# Include
# Include
Using namespace std; char * my_strtok (char * dist, const char * src) {static char * result; // save dist with static variables here, // to meet the implementation of the strtok (NULL,) library function. Char map [32]; const char * p = src; memset (map, 0, sizeof (map); while (* p) {map [* p> 3] | = (0x1 <(* p & 0x7); p ++ ;} // There are only 256 characters in ASCII, So we map src to 32 char arrays, // then, the lower 7 bits are taken as one of the eight bits in the array, and the position 1 indicates that the position exists. Here we need to ensure the unique one-to-one ing relationship. The 32 char Arrays can satisfy our needs. Char * q; if (dist) q = dist; else q = result; // if the first parameter passed in from the outside is NULL, then we start with the end position of the previous parameter //. The saved result variable is used here. While (map [* q> 3] & (1 <(* q & 0x7) & * q) q ++; // remove the redundant separator. Dist = q; for (; * dist; dist ++) {if (map [* dist> 3] & (0x1 <(* dist & 0x7) {* dist = ''; dist ++; break;} // place the delimiter for the first time, and then stop break.} Result = dist; // The result is used to save the next position of the last operation. // that is, if NULL is input as the first parameter, the result starts. Return q;} int main () {char s [] = abc efg hlk; cout <my_strtok (s,) <endl; cout <