您的位置:首页 > 编程语言 > C语言/C++

c++第二周任务三:/*(3‐1)输入一行字符,统计其中有多少个单词

2012-02-29 17:22 399 查看
 任务三:

/*(3‐1)输入一行字符,统计其中有多少个单词。每两个单词之间以空格(可能多个)隔

开,或者由标点符号(只考虑 , . ; ? !共5 种)隔开。 如输入: How old are your? I am

20. 输出:There are 7 words in the line. 【知识点:字符数组】

* 算法说明:

*/

#include <iostream>

#include "string.h"

using namespace std;

int main()

{

}

 

#include <iostream>

#include"string.h"

using namespace std;

int main()

{

 char str[50];

 gets(str);

 int i,num=0;

 for (i=0;str[i]!='\0';i++)

 {

  if(str[i]==' ')

  {

   num++;

   while(str[i+1]==' ')

   {

    i++;

   }

  }

 }

 cout<<"There are "<<num+1<<" words in the line"<<endl;

 return 0;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  任务 c++ 算法
相关文章推荐