高仿模拟练习,补全 found 标记处空缺,答案点击按钮展开
统计字符串中单词个数,单词之间用空格隔开,请补全found。
#include <stdio.h>
int fun(char *s)
{
int cnt=0,flag=1;
while(*s!='\0')
{
/**********found**********/
if(*s!=' ' && ___1___ )
{
/**********found**********/
cnt = cnt + ___2___;
flag = 0;
}
else if(*s == ' ')
/**********found**********/
___3___;
s++;
}
return cnt;
}
int main()
{
char str[128];
gets(str);
printf("words=%d\n",fun(str));
return 0;
}
flag
1
flag=1
第1空判断当前字符不是空格,前一个是空格代表新单词;第2空单词计数+1;第3空更新标记记录当前字符是否空格。输入"hello c world"输出words=3。