填空题
1. 请补充main()函数,该函数的功能是:从一个字符串中截取前面若干个给定长度的子字符串。其中,str1指向原字符串,截取后的字符存放在str2所指的字符数组中,n中存放需截取的字符个数。
例如,str1="abcdefghi",然后输入7,则str2="abcdefg"。
注意:部分源程序如下。
请勿改动main()函数和其他函数中的任何内容,仅在横线上填入所编写的若干表达式或语句。
试题程序: #include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#define N 80
void main()
{
char str1[N],str2[N];
int n,i;
system("CLS");
printf("Enter the string:\n");
gets(str1);
printf("Enter the position of the string
deleted:");
scanf(______);
for(i=0;i<n;i++)
______
str2[i]='\0';
printf("The new string js:%s\n", ______);
}
【正确答案】
1、"%d",&n
str2[i]=str1[i];
str2
【答案解析】 由程序可知,变量n中存放截取字符个数的变量,应从键盘输入其数值,因此,第一空处填“"%d",&n”;将字符串str1中的前n个字符放入字符串str2中,因此,第二空处填“str2[i]=str1[i];”;最后,要输出新的字符串“str2”,因此,第三空处填str2。