题目:输入一行文本,求文本中的每个单词的长度。
以 word length
this 4
的格式输出
代码:
//输入一行文本,求文本中的每个单词的长度。
//以 word length
// this 4
//的格式输出
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
using namespace std;
int main(){
char s[20];
gets(s);
char *word = strtok(s," ");
while (word) {
cout << word << " " << strlen(word) << endl;
//第一次提取子串完毕之后,继续对源字符串s进行提取,应在其后(第二次,第三次。。。第n次)
//的调用中将strtok的第一个参数赋为空值NULL(表示函数继续从上一次调用隐式保存的位置,继续分解字符串;
//对于前一次次调用来说,第一次调用结束前用一个this指针指向了分界符的下一位)
word = strtok(NULL, " ");
}
return 0;
}
结果: