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

来源:互联网 发布:ps修复软件 编辑:程序博客网 时间:2024/05/08 22:17

 任务三:
/*(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;
}

原创粉丝点击