1、二级 C 语言-284 (1)及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充 main()函数,该函数的功能是:把字符串 str1 中的非空格字符复制到字符串 str2 中。 例如,若 str1=“nice to meet you!“, 则 str2=“nicetomeetyou!“。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在 main()函数的横线上填入所编写的若干表达式或语句。 试题程序: #includestdlib.h #includestdio.h #define M 80 vo
2、id main() static char str1M=“nice to meet you!“; char str2M; int i=0, j=0; system(“CLS“); printf(“/n*str1*/n“); puts(str1); while(str1i) if( 1) str2j+=str1i; 2; printf(“/n*str2*/n“); for(i=0; ij; i+) printf(“%c“, str2i); (分数:30.00)二、程序改错题(总题数:1,分数:40.00)2.下列给定的程序中,函数 proc()的功能是:将 str 所指字符串中出现的 t1 所指
3、字符串全部替换成 t2所指字符串,所形成的新的字符串放在 w 所指的数组中。在此处,要求 t1 和 t2 所指字符串的长度相同。 例如,当 str 所指字符串中所指的内容为 abcdabcdefg,t1 所指字符串中的内容为 bc,t2 所指字符串中的内容为 11 时,结果在 w 所指的数组中的内容应为 a11da11defg。 请修改程序中的错误,使它能得出正确的结果。 注意:不要改动 main()函数。不得增行或删行,也不得更改程序的结构。 试题程序: #includestdlib.h #incIudeconio.h #includestdio.h #includestring.h /*f
4、ound* int proc(char*str, char*t1, char*t2, char*w) char *p, *r, *a; strcpy(w, str); while(*w) p=w; r=t1; /*found* while(r) if(*r=*p)r+; p+; else break; if(*r=“/0“) =w; r=t2; /*found* while(*r)*a=*r; a+; r+ w+=strlen(t2); else w+; void main() char str100, t1100, t2100, w100; system(“CLS“); printf(“/n
5、Please enter string str:“); scanf(“%s“, str); printf(“/nPlease enter substring t1:“); scanf(“%s“, t1); printf(“/nPlease enter substring t2:“); scanf(“%s“, t2); if(strlen(t1)=strlen(t2) proc(str, t1, t2, w); printf(“/nThe result is: %s/n“, w); else printf(“Error: strlen(t2)/n“); (分数:40.00)_三、程序设计题(总题
6、数:1,分数:30.00)3.假定输入的字符串中只包含字母和*号。请编写函数 proc(),它的功能是:除了尾部的*号之外,将字符串中其他*号全部删除。形参 p 已指向字符串中最后一个字母。在编写函数时,不得使用 C 语言的字符串函数。 例如,若字符串中的内容为*a*bc*def*g*,删除后,字符串中的内容应当是 abcdefg*。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includeconio.h #includestdio.h void proc(char *str, char
7、*p) void main() char str81, *t; printf(“Enter a string:/n“); gets(str); t=str; while(*t) t+; t-; /指针 t 指向字符串尾部 while(*t=“*“) t-; /指针 t 指向最后一个字母 proc(str, t); printf(“The string after deleted:/n“); puts(str); (分数:30.00)_二级 C 语言-284 (1)答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充 main()函数,该函
8、数的功能是:把字符串 str1 中的非空格字符复制到字符串 str2 中。 例如,若 str1=“nice to meet you!“, 则 str2=“nicetomeetyou!“。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在 main()函数的横线上填入所编写的若干表达式或语句。 试题程序: #includestdlib.h #includestdio.h #define M 80 void main() static char str1M=“nice to meet you!“; char str2M; int i=0, j=0; system(
9、“CLS“); printf(“/n*str1*/n“); puts(str1); while(str1i) if( 1) str2j+=str1i; 2; printf(“/n*str2*/n“); for(i=0; ij; i+) printf(“%c“, str2i); (分数:30.00)解析:str1i!=“ i+解析 题目中要求把字符串 str1 中的非空格字符复制到字符串 str2 中,因此if 语句的条件是判断 str1 中的字符不是空格,因此,第一处填“str1i!=“”;由程序可知,变量 i为字符串 str1 中字符的下标,检查字符串 str1 中的字符通过改变元素下标变量
10、来实现,因此,第二处填“i+”。二、程序改错题(总题数:1,分数:40.00)2.下列给定的程序中,函数 proc()的功能是:将 str 所指字符串中出现的 t1 所指字符串全部替换成 t2所指字符串,所形成的新的字符串放在 w 所指的数组中。在此处,要求 t1 和 t2 所指字符串的长度相同。 例如,当 str 所指字符串中所指的内容为 abcdabcdefg,t1 所指字符串中的内容为 bc,t2 所指字符串中的内容为 11 时,结果在 w 所指的数组中的内容应为 a11da11defg。 请修改程序中的错误,使它能得出正确的结果。 注意:不要改动 main()函数。不得增行或删行,也不
11、得更改程序的结构。 试题程序: #includestdlib.h #incIudeconio.h #includestdio.h #includestring.h /*found* int proc(char*str, char*t1, char*t2, char*w) char *p, *r, *a; strcpy(w, str); while(*w) p=w; r=t1; /*found* while(r) if(*r=*p)r+; p+; else break; if(*r=“/0“) =w; r=t2; /*found* while(*r)*a=*r; a+; r+ w+=strlen
12、(t2); else w+; void main() char str100, t1100, t2100, w100; system(“CLS“); printf(“/nPlease enter string str:“); scanf(“%s“, str); printf(“/nPlease enter substring t1:“); scanf(“%s“, t1); printf(“/nPlease enter substring t2:“); scanf(“%s“, t2); if(strlen(t1)=strlen(t2) proc(str, t1, t2, w); printf(“
13、/nThe result is: %s/n“, w); else printf(“Error: strlen(t2)/n“); (分数:40.00)_正确答案:()解析:(1)错误:int proc(char*str, char*t1, char*t2, char*w) 正确:void proc(char*str, char*t1, char*t2, char*w) (2)错误:while(r) 正确:while(*r) (3)错误:r+ 正确:r+; 解析 由主函数中 proc()函数的调用以及 proc()函数的定义,可知该函数没有返回值,因此把 proc()前的“int”改为“void”
14、;由 proc()函数可知,变量 r 指向的是字符串 t1 地址,while 循环要判断的是字符串 t1 是否结束,因此,“while(r)”应改为“while(*r)”;C 语言中,每一条语句都以分号结束,因此,“r+”后要加上分号。三、程序设计题(总题数:1,分数:30.00)3.假定输入的字符串中只包含字母和*号。请编写函数 proc(),它的功能是:除了尾部的*号之外,将字符串中其他*号全部删除。形参 p 已指向字符串中最后一个字母。在编写函数时,不得使用 C 语言的字符串函数。 例如,若字符串中的内容为*a*bc*def*g*,删除后,字符串中的内容应当是 abcdefg*。 注意:
15、部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includeconio.h #includestdio.h void proc(char *str, char *p) void main() char str81, *t; printf(“Enter a string:/n“); gets(str); t=str; while(*t) t+; t-; /指针 t 指向字符串尾部 while(*t=“*“) t-; /指针 t 指向最后一个字母 proc(str, t); printf(“The st
16、ring after deleted:/n“); puts(str); (分数:30.00)_正确答案:()解析:void proc(char*str, char*p) char*t=str; for(; t=p; t+) if(*t!=“*“) /把前面不是*号的字符放到 a 数组中,通过 t 的移动来实现 *(str+)=*t; for(; *t!=“/0“; t+) *(str+)=*t; /把尾部的*号接到其后面,当*t 为“/0“时结束 *str=“/0“; /题目要求 a 数组中存放字符,因此用“/0“作为结束标志 解析 题目中要求除了尾部的*号之外,将字符串中其他*号全部删除。首先将所有不是*的字符放在字符串 str 中,然后将尾部的*接在字符的后面,最后为字符串 str 加上结束符。