请保证代码工整,关键部分请用注释对代码逻辑进行说明
不调用任何库函数实现stat_word,该函数的功能是从字符串str中统计单词个数,str完全由英文字母及空格符组成,连续出现的若干个非空格字符即为一个单词。(5分)
int stat_word(const char* str);
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int stat_word(const char* str)
{
int num=0;
int word=0;
while(*str!='\0')
{
if(word==0)
{
if(*str++!=' ')
{
num++;
word=1;
}
}
else if(*str++==' ')
{
word=0;
}
}
return num;
}
int main()
{
cout<<stat_word("hello, World!")<<endl;
return 0;
}