C ++ strtok () Implementation Function Analysis

Source: Internet
Author: User

The C ++ programming language is flexible and supports various programming styles. Here we will introduce how to use C ++ strtok () to implement the functions of the Split function. We hope you can experience the power of this language.

  • Summary of C ++ multi-thread testing
  • Detailed description of the C ++ explicit keyword Application Method
  • C ++ function Overloading is implemented through C Language
  • Questions about C ++ polymorphism coverage
  • Overview of C ++ streaming File Operations

Related functions: index, memchr, rindex, strpbrk, strsep, strspn, strstr

Header file: # include <string. h>

The Split function in C/C ++ is C ++ strtok (). Its prototype is as follows:

 
 
  1. char * strtok (char * str, const char * delimiters);  

Function Description

C ++ strtok () is used to split strings into segments. The str parameter points to the string to be split, and the delimiters parameter is the split string. When strtok () when the delimiters Delimiter is found in the str string, the Delimiter is changed to '\ 0. In the first call, strtok () must be given the str string parameter. In future calls, the str parameter is set to NULL. If each call is successful, the next split string pointer is returned.

Return Value

Returns the string pointer after the next split. If no split exists, NULL is returned.

Example-1

 
 
  1. /* strtok example */   
  2. #include <stdio.h>   
  3. #include <string.h>   
  4. int main ()   
  5. {   
  6. char str[] ="a,b,c,d*e";   
  7. const char * split = ",";   
  8. char * p;   
  9. p = strtok (str,split);   
  10. while(p!=NULL) {   
  11. printf ("%s\n",p);   
  12. p = strtok(NULL,split);   
  13. }   
  14. getchar();   
  15. return 0;   

In this example, the strings 'a, B, c, d * e "are separated by commas (,) as delimiters. The output result is as follows:

 
 
  1. a   
  2. b   
  3. c   
  4. d*e 

Because delimiters supports multiple delimiters, we use the statement lines in this example.

 
 
  1. const char * split = ",";  

Change

 
 
  1. Const char * split = ", *"; // use commas (,) and asterisk (*) to separate strings.

The output result is as follows:

 
 
  1. a   
  2. b   
  3. c   
  4. d   

Example 2:

 
 
  1. #include <string.h>   
  2. #include <stdio.h>   
  3. #include <stdlib.h>   
  4. char string[] = "1:ip:ipport:user ";   
  5. char seps[] = ": ";   
  6. char *token;   
  7. int main( void )   
  8. {   
  9. printf( "Tokens:\n " );   
  10. // Establish string and get the first token:   
  11. token = strtok( string, seps ); // C4996   
  12. // Note: strtok is deprecated; consider using strtok_s instead   
  13. while( token != NULL )   
  14. {   
  15. // While there are tokens in "string "   
  16. printf( "%s\n ", token );   
  17. // Get next token:   
  18. token = strtok( NULL, seps ); // C4996   
  19. }   
  20. system( "pause ");   
  21. return 0;   

The preceding section describes C ++ strtok.

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.