编程好手来
输入一行字符,统计其中有多少单词(单词间以空格分隔)。比如:输入"I am a boy."有四个单词。
用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
请按任意键继续. . .