char*strtok(char*strTOKen,const char*strDelimit);
第一个参数:字符串
第二个参数:字符串的分隔符利用strtok函数进行分割
/*计算字符串中的单词数目,并将单词输出出来*/ void calculate(char* str) { int count = 0;//计数器 const char* sep = " .,";//存储分割符 char* tok = nullptr;//指针,指向字符串首个字符 for (tok = strtok(str, sep); tok != nullptr; tok = strtok(nullptr, sep)) { count++; cout << tok << endl; } cout << count; } void main() { char str[] = "I am. a, student"; calculate(str); }
运行结果: