请在程序的下划线处填入正确的内容并把下划线删除, 使程序得出正确的结果。
注意:源程序存放在考生文件夹下BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
#include
#define N 5
#define M 10
int fun(char (*ss)[M], int k)
{int i,j=0,len;
/**********found**********/
for(i=0; i< __1__ ; i++)
{len=strlen(ss[i]);
/**********found**********/
if(len<= __2__)
/**********found**********/
strcpy(ss[j++],__3__);
}
return j;
}
main()
{char x[N][M]={"Beijing","Shanghai","Tianjing", "Nanjing","Wuhan"};
int i,f;
printf("\nThe original string\n\n");
for(i=0;i f=fun(x,7); printf("The string which length is less than or equal to 7 :\n"); for(i=0; i } 解题答案: /**********第一空**********/ for(i=0; i< N ; i++) /**********第二空**********/ if(len<= k) /**********第三空**********/ strcpy(ss[j++],ss[i]); ****************************************** [NextPage] 二、改错题:给定程序MODI1.C中函数fun的功能是:逐个比较p、q所指两个字符串对应位置中的字符,把ASCII值大或相等的字符依次存放到c所指数组中,形成一个新的字符串。 例如,若主函数中a字符串为:aBCDeFgH,主函数中b字符串为:ABcd,则c中的字符串应为:aBcdeFgH。 请改正程序中的错误,使它能得出正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 给定源程序: #include #include void fun(char *p ,char *q, char *c) { /************found************/ int k = 1; /************found************/ while(*p != *q) {if(*p<*q) c[k]=*q; else c[k]=*p; if(*p) p++; if(*q) q++; k++; } } main() {char a[10]="aBCDeFgH", b[10]="ABcd", c[80]= {'\0'}; fun(a,b,c); printf("The string a: "); puts(a); printf("The string b: "); puts(b); printf("The result : "); puts(c); } 解题答案: /************found************/ int k=0; /************found************/ while(*p||*q) ****************************************** [NextPage] 三、程序题:假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:除了字符串前导的*号之外,将串中其它*号全部删除。在编写函数时,不得使用C语言提供的字符串函数。函数fun中给出的语句仅供参考。 例如,字符串中的内容为:****A*BC*DEF*G*******,删除后, 字符串中的内容应当是:****ABCDEFG。 注意: 部分源程序在文件PROG1.C文件中。 请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 给定源程序: /#include void fun(char *a) { } main() {char s[81];void NONO (); printf("Enter a string:\n");gets(s); fun(s); printf("The string after deleted:\n");puts(s); NONO(); } void NONO () {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *in, *out ; int i ; char s[81] ; in = fopen("in.dat","r"); out = fopen("out.dat","w"); for(i = 0 ; i < 10 ; i++) { fscanf(in, "%s", s); fun(s); fprintf(out, "%s\n", s); } fclose(in); fclose(out); } 参考答案: void fun( char *a ) { /* 以下代码仅供参考 */ int i=0,k; while(a[i]=='*') i++; k=i; while(a[i]!='\0') /* 以下程序段实现非*字符前移 */ { if(a[i] != '*') a[k++]=a[i]; i++; } a[k]='\0'; }
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|