用C++来编
#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
int b=0,word=0;
char ch;
cout<<"input the scentence!"<<endl;
do{
ch=getche();
if(b)
{
if(ch==' ') b=0;
else b=1;
}
else
{
word++;
b=1;
}
}while(ch!='\r');
cout<<"the word number is "<<word<<endl;
system("pause");
return 0;
}
若键入I am a boy.
结果如下
input the scentence!
the word number is 4
请按任意键继续. . .