给定程序中,函数fim的功能是:计算出形参s所指字符串中包含的单词个数,作为函数值返回。为便于统计,规定各单词之间用空格隔开。 例如,形参s所指的字符串为:This is a C language program.,函数的返回值为6。 请在程序

admin2017-11-27  21

问题   给定程序中,函数fim的功能是:计算出形参s所指字符串中包含的单词个数,作为函数值返回。为便于统计,规定各单词之间用空格隔开。
  例如,形参s所指的字符串为:This is a C language program.,函数的返回值为6。
    请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
  注意:源程序存放在考生文件夹下的BLANK1.C中。
  不得增行或删行,也不得更改程序的结构!
#include
int fun(char *s)
{ int n=0, flag=0;
while(*s!=’\0’)
{ if(*s!=’ ’ && flag==0) {
/**********found**********/
【1】 ; flag=l;}
/********** found**********/
if (*s==’ ’) flag= 【2】 ;
/********** found**********/
【3】 ;
}
return n;
}
main ()
{ char str[81]; int n;
printf("\nEnter a line text:\n");
gets (str);
n=fun(str);
printf("\nThere are %d words in
this text.\n\n",n);
}

选项

答案(1)n++ (2)0 (3)s++

解析 函数fun的功能是计算出形参s所指字符串中包含的单词个数。
    第一空:“if(*s!=’’&&flag==0)”说明找到空格了,单词的数量应加1,故第一空处应为“n++”。
  第二空:“if(*s!=’’&&flag==0)”和“if(*s==’’)flag=【2】;”在flag为0的情况下,n才加1,因此,第二空处是将flag置0,即第二空处应为“0”。
    第三空:“while(*s!=’\0’)”循环的终止条件是s达到结尾,因此,在循环体内s应该不断往字符串尾移动,即第三空为“s++”。
转载请注明原文地址:https://kaotiyun.com/show/vnxp777K
0

最新回复(0)