/*(文件注释头部开始)
*程序的版权和版本声明部分
*Copyright (c) 2011,烟台大学计算机学院学生
*All rights reserved.
*文件名称:字符串中单词个数的统计
*作 者:2011级计114-3 苗影
*完成日期:2011年3月3号
*版本号:vc.3
* 对任务及求解方法的描述部分
* 输入描术:输入一行字符串
* 问题描述: 每两个单词之间以空格(可能多个)隔开,
*或由标点符号(只考虑,.;?!5种)隔开
当输入 I am come from shandong时
* 程序输出:输出单词的个数
* 程序头部的注释结束
*/
#include<iostream>
using namespace std;
int main()
{
char str[100];
int i = 0, num = 0;
bool isword = false;
gets(str);//不用cin,如果用cin的话,输入的字符串,遇到空格就会结束
while(str[i]!='\0')
{
if(str[i] == ' ')
{
isword = false;
}
else if(isword == false)
{
isword = true;
++num;
}
++i;
}
cout << "There are " << num << " words in the line" << endl;
return 0;
}
贴图结果