输入字符串: i am a boy
单词数有4个
输入字符串:hong gil dong
单词数有3个
输入字符串: i am a boy
单词数有4个
输入字符串:hong gil dong
单词数有3个
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
char ch[1010];
int main()
{
int x = 0;
printf("输入字符串:");
scanf("%[^\n]", ch);
char *t = strtok(ch, " ");
while (t != NULL)
{
x++;
t = strtok(NULL, " ");
}
cout << "单词个数有" << x << "个";
return 0;
}