填空题
1. 请补充main()函数,该函数的功能是:从键盘输入一组字符串,以“*”结束输入,并显示出这个字符串。
例如,输入“ABCDEFG*”,结果显示“ABCDEFG”。
注意:部分源程序如下。
请勿改动main()函数和其他函数中的任何内容,仅在main()函数的横线上填入所编写的若干表达式或语句。
试题程序: #include<stdlib.h>
#include<stdio.h>
#define M 80
void main()
{
int i=-1,j=0;
char str[M];
system("CLS");
printf("\n Input a string\n");
do
{
i++;
scanf(______);
}
while(______);
printf("\n**display the string**\n");
while(j<i)
{
printf(______);
j++;
}
}
【正确答案】
1、"%c",&str[i]
str[i]!='*'
"%c", str [j]
【答案解析】 由程序可知,从键盘输入的字符串存放在字符串变量str中,因此,第一空处填“"%c",&str[i]”。题目要求字符串以“*”号结束,语句while中的条件为字符串不结束的条件,因此,第二空处填“str[i]!='*'”。printf用来格式化输出字符串str,变量j用来控制字符串的下标,因此,第三空处填“"%c",str[j]”。