利用C++如何编程:编写程序,统计一串字符中包含的单词数(假定单词以一个或多个空格分隔)。

来源:百度知道 编辑:UC知道 时间:2024/06/07 11:23:43
源码,谢谢

可以这样的解决你的这个问题。编译并且通过

#include <iostream.h>
#include <stdio.h>
void main()
{
char string[100];
int num=0,word=0;
char c;
gets(string);

for(int i=0;(c=string[i])!='\0';i++)

if (c==' ') word=0;
else if(word==0)
{
word=1;
num++;
}

cout<<"there are " <<num <<" words in the string\n ";
}