c语言编程题:输入一行字符,统计其中有多少个单词,单词之间用逗号分开

2024-11-06 12:46:05
推荐回答(1个)
回答1:

//-----------------------------------------------------
// C++APP,统计一段话中英语单词个数!
//------------------------------------------------------
#include "iostream.h"

#include "stdio.h" //包含gets函数

int main()
{

char string[10000];

int i;

int num = 0; /* 统计单词个数 */

bool word; /* 判定是否为单词 */

word = false;

char c;

cout<<"Input sentence,Please."<
gets(string);

for (i = 0; (c = string[i]) != '\0'; i++) //以回车'ENTER'作为结束标志!
{
if (c == ' ')

word = 0;

else

if (word == false)
{
word = true;

num++;
}
}

cout<<"The sum of words is:"<
return 0;
}