问题描述:编写一个程序,输入一个字符串(长度不超过 80) ,然后统计出该字符串当中包
含有多少个单词。例如:字符串“this is a book”当中包含有 4 个单词。
输入格式:输入一个字符串,由若干个单词组成,单词之间用一个空格隔开。
输出格式:输出一个整数,即单词的个数。
输入输出样例
用户输入数据样例:
this is a book
系统输出数据如下:
含有多少个单词。例如:字符串“this is a book”当中包含有 4 个单词。
输入格式:输入一个字符串,由若干个单词组成,单词之间用一个空格隔开。
输出格式:输出一个整数,即单词的个数。
输入输出样例
用户输入数据样例:
this is a book
系统输出数据如下:
4
//*************************单词个数统计P-107
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
//analysis_the_word函数用于分解统计字符数组c的各个单词,并返回单词个数
int analysis_the_word(char *c)
{
int count=0;
for(int i=0;i<strlen(c)+1;i++)
{
if(c[i]==' '||c[i]=='\0')
{
count++;
}
}
return count;
}
int main()
{
char c[81];
gets(c);
if(strlen(c)<=80)
{
cout << analysis_the_word(c);
}
return 0;
}