问题及代码:
/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:num.cpp
*作 者:单昕昕
*完成日期:2014年12月15日
*版 本 号:v1.0
*
*问题描述:统计句子中单词的个数。
*程序输入:无。
*程序输出:句子中单词的个数。
*/
#include <iostream>
using namespace std;
int pwordnum(char *str);
int main()
{
char s[100]="What hurts more, the pain of hard work, or the pain of regret? ";
pwordnum(s);
cout<<s<<endl;
cout<<"单词的个数为:"<<pwordnum(s)<<endl;
return 0;
}
int pwordnum(char *str)
{
char *p=str;
int count=0;
for(; *p!='\0'; p++)
{
if(*p==' ')
count++;
}
return (count);
}
运行结果:
统计句子中单词的个数。
学习心得:
又出现了失误!!!哭死啊~~
*p==‘ ’忘记==了!!count忘记赋初值了!!
以后再也不要犯这种低级错误了!!