1.填空題
請補充main函數,該函數的功能是:從一個字符串中截取前面若干個給定長度的子字符串。其中,str1指向原字符串,截取后的字符存放在str2所指的字符數組中,n中存放需截取的字符個數。
例如:當str1=“cdefghij”,然后輸入4,則str2=“cdef”。
注意:部分源程序給出如下。
請勿改動主函數main和其他函數中的任何內容,僅在函數fun()的橫線上填入所編寫的若干表達式或語句。
試題程序:
#include
#include
#define len 80
main()
{
char str1[len],str2[len];
int n,i;
clrscr();
printf("enter the string:\n");
gets(str1);
printf("enter the position of the string
deleted:");
scanf(【1】);
for(i=0;i 【2】
str2[i]=’\0’;
printf("the new string is:%s\n",【3】);
1.填空題
【1】″%d″,&n 【2】str2[i]=str1[i]; 【3】str2
【解析】填空1:本題考查對標準輸入函數scanf()的調用格式,由后面的程序可以知道,變量n保存了要截取的字符數,注意在n前面不要忘了取址符‘ &’。填空2:截取前n個字符,就是將字符串str1的前n個字符依次賦給字符串str2的前n個字符。填空3:本題考查對標準輸出函數printf()的調用格式,根據題意,應輸出截取后的字符串,即字符串str2
請補充main函數,該函數的功能是:從一個字符串中截取前面若干個給定長度的子字符串。其中,str1指向原字符串,截取后的字符存放在str2所指的字符數組中,n中存放需截取的字符個數。
例如:當str1=“cdefghij”,然后輸入4,則str2=“cdef”。
注意:部分源程序給出如下。
請勿改動主函數main和其他函數中的任何內容,僅在函數fun()的橫線上填入所編寫的若干表達式或語句。
試題程序:
#include
#include
#define len 80
main()
{
char str1[len],str2[len];
int n,i;
clrscr();
printf("enter the string:\n");
gets(str1);
printf("enter the position of the string
deleted:");
scanf(【1】);
for(i=0;i
str2[i]=’\0’;
printf("the new string is:%s\n",【3】);
1.填空題
【1】″%d″,&n 【2】str2[i]=str1[i]; 【3】str2
【解析】填空1:本題考查對標準輸入函數scanf()的調用格式,由后面的程序可以知道,變量n保存了要截取的字符數,注意在n前面不要忘了取址符‘ &’。填空2:截取前n個字符,就是將字符串str1的前n個字符依次賦給字符串str2的前n個字符。填空3:本題考查對標準輸出函數printf()的調用格式,根據題意,應輸出截取后的字符串,即字符串str2

