C++循环结构统计单词个数

2025-05-12 13:14:31
推荐回答(1个)
回答(1):

给你个C语言的,要去忙了,来不及帮你改成C++的,你自己改把
如果不行,等我回来帮你改了
#include
int main()
{
char string[81];
int i;
int num = 0; /* 统计单词个数 */
int word = 0; /* 是否为单词的标示 */
char c;
printf("Enter a sentence!");
gets(string);

for (i = 0; (c = string[i]) != '\0'; i++)
{
if (c == ' ')
word = 0;
else
if (word == 0)
{
word = 1;
num++;
}
}
printf("\nThere are %d words int the line.\n", num);
return 0;
}