Strtok () delimiter string Function

Source: Internet
Author: User
Tags strtok

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 <
    
   
  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.