分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL
#include<iostream>
#include<cstring>
using
namespace
std;
int
main()
{
char
sentence[]=
"This is 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=" << tokenPtr<<endl;return0;
}