Introduction to the use of strtok () function in C + + _c language

Source: Internet
Author: User
function Prototypes: Char *strtok (char *s, const char *delim);
Function:The exploded string is a set of strings. S is the string to be decomposed, Delim as the delimiter string.
Description:Strtok () is used to split a string into fragments. The argument s points to the string to be split, and the parameter Delim is a split string, and the Strtok () is found in the string of parameter s to the split word characters of the parameter Delim. On the first call, the Strtok () must give the parameter S string, and the subsequent call sets the parameter s to null. A pointer to a segmented fragment is returned each time a call succeeds.
Copy Code code as follows:

#include <iostream>
#include <cstring>
using namespace Std;
int main ()
{
Char sentence[]= "This are a sentence with 7 tokens";

cout<< "The string to be tokenized is:\n"
<<sentence<< "\n\nthe tokens are:\n\n";

Char *tokenptr=strtok (sentence, "");

while (Tokenptr!=null) {
cout<<tokenptr<< ' \ n ';
Tokenptr=strtok (NULL, "");
}

cout<< "After strtok, sentence =" <<sentence<<endl;
return 0;
}
/* Function The first call needs to set two parameters. The result of the first split, which returns the first ', ' string of strings, which is the first time the program outputs ABC.
* The second call to the function strtok (null, ","), the first parameter is set to NULL. The result returns the string after which the partition is based, that is, the second output d.
* Strtok is a thread-unsafe function because it uses statically allocated space to store the split string position
* Thread-safe function called Strtok_r,ca
* When using strtok to determine IP or Mac, it is important to use other methods to determine '. ' or ': ' The number of,
* Because with strtok truncation, for example: "192..168.0...8 ..." This string, Strtok will only intercept four times, the middle of ... No matter how many will be treated as a key
*/

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.