问题及代码:
/*
*Copyright (c) 2014,烟台大学计算机学院
*All gight reserved.
*文件名称:temp.cpp
*作者:曾建强
*完成时间:2014年12月12日
*版本号:v1.0
*问题描述:统计str中单词的个数
*输入描述:
*输出描述:
*/
#include <iostream>
using namespace std;
int pwordnum(char *str);
int main()
{
char s1[50]="Hello world.My name is liyingjie. ";
char s2[50]="Good morning.";
char s3[50]="vegetable bird!";
cout<<s1<<":"<<pwordnum(s1)<<'\n';
cout<<s2<<":"<<pwordnum(s2)<<'\n';
cout<<s3<<":"<<pwordnum(s3)<<'\n';
return 0;
}
int pwordnum(char *str)
{
int i,j=1;
for(i=0;*(str+i)!='\0';i++)
{
if(*(str+i)==' ')j++;
}
return j;
}
运行结果: