/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:gcd.cpp
*作 者:惠睿
*完成日期:2014年12月16日
*版 本 号:v1.0
*
*问题描述:用指针作形参求出单词的个数。
*程序输出:输出单词的个数。
*/
#include <iostream>
using namespace std;
int pwordnum(char *str);
int main()
{
char s1[50]="Hello world. ";
char s2[50]="Good morning. ";
char s3[50]="vegetable bird! ";
cout<<"单词的个数为:"<< pwordnum(s1)<<" "<<pwordnum(s2)<<" "<< pwordnum(s3)<<endl;
}
int pwordnum(char *str)
{
int n=0;
while(*str!='\0')
{
if(*str==' ')
{
n++;
}
str++;
}
return n;
}
运行结果:
知识点总结:熟悉指针的运用。