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
*/