/*
Copyright (c) 烟台大学计算机与控制工程学院
Author:刘慧艳
Created:
Edition:V1.0
Describe:统计句子str中单词的个数
用数组名作形参:int awordnum(char str[])
用指针作形参:int pwordnum(char *str)
*/
#include <iostream>
#include <string>
using namespace std;
int awordnum(char str[]);
int pwordnum(char *str);
int main()
{
char str[50]="Crystal is xiao xiao bai si mi da";
cout<<awordnum(str)<<endl;
cout<<pwordnum(str)<<endl;
return 0;
}
int awordnum(char str[])
{
int i,num=0,word=0;
for(i=0;(str[i]!='\0');++i)
{
if(str[i]==' ')
word=0;
else
if(word==0)
{
word=1;
num++;
}
}
return num;
}
int pwordnum(char *str)
{
int num=0,word=0;
char *p;
for(p=str;*p!='\0';++p)
{
if(*p==' ')
word=0;
else
if(word==0)
{
word=1;
num++;
}
}
return num;
}
用指针玩字符串--单词个数
最新推荐文章于 2021-05-22 13:25:22 发布