填空题
C语言
题号 #2256615
请补充main()函数,该函数的功能是:从一个字符串中截取前面若十个给定长度的子字符串。其中,str1指向原字符串,截取后的字符存放在str2所指的字符数组中,n中存放需截取的字符个数。
例如,当str1="abcdefghi",然后输入7,则str2="abcdefg”。
注意:部分源程序给出如下。
清勿改动main()函数和其他两数中的任何内容,仅在横线上填入所编写的若干表达式或语句。
试题程序:
#iuelude<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(【1】);
for(i=0:i<n;i++)
【2】
str2[i]='\0';
printf("The new string is:%s\n",【3】);
}
参考答案
暂无
答案解析
【1】"%d",&n【2】str2[i]=str1[i]:【3】str2
【解析】由程序可知,变量n中存放截取字符个数的变量,因此应从键盘输入其数值,因此,【1】处填“"%d",&n”;将字符串str1中前n个字符放入字符串str2中,因此,【2】处填“str2[i]=str1[i];”;最后要输出新的字符串“str2”,因此.【3】处埴str2。