C语言编程03.docVIP

  • 7
  • 0
  • 约4.48千字
  • 约 6页
  • 2017-05-12 发布于河南
  • 举报
C语言编程03

题目 填空题 请补充函数fun(),该函数的功能是:把从主函数中输入的字符串str2接在字符串str1的后面。 例如:str1=“How do”,str2=“ you do?”,结果输出:How do you do? 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。(后面各题要求相同) 试题程序: #includestdio.h #includeconio.h #define N 40 void fun(char *str1,char *str2) { int i=0; char *p1=str1; char *p2=str2; while(*(p1+i)) i++; for( ;*p2【2】;i++) *(p1+i)=*p2++【3】; *(p1+i)=\0; } main() { char str1[N],str2[N]; clrscr(); printf(*****Input the string str1 str2*****\n); printf( \nstr1:); gets(str1); printf( \nstr2:); gets(str2); printf(**The string str1 str2**\n); puts(str1); puts(str2); fun(str1,str2); printf(*****The new string *****\n); puts(str1); } 答案 知识点 评析 填空1:变量i用来记录字符串str1的长度,当指针指到字符串str1结束标志符‘\0时,while循环结束,变量i停止累加。 填空2:指针p2指向字符串str2,通过for循环将字符串str2接在str1后面,循环结束的条件是指针p2所指的字符是字符串结束标志符‘\0。 填空3:指针p2最初指向字符串str2的首字符,通过自加1,使指针p2依次向后移动,指向str2的各个字符,实现将字符串str2接在str1后面的功能。 相关知识 题目 改错题 下列给定程序中,函数fun()的作用是:将字符串tt中的小写字母都改为对应的大写字母,其他字符不变。例如,若输入edS,dAd,则输出EDS,DAD。 请改正程序中的错误,使它能得到正确结果。 试题程序: #include stdio.h #include string.h #include conio.h /**********************found***********************/ char fun(char tt[]) { int i; for(i=0;tt[i];i++) { /**********************found***********************/ if((tt[i]=A)(tt[i]= Z)) tt[i]-=32; } return(tt); } main() { int i; char tt[81]; clrscr(); printf(\nPlease enter a string: ); gets(tt); printf(\nThe result string is: \n%s,fun(tt)); } 答案 知识点 评析 错误1:函数的返回值是字符串的首地址,是指针类型,所以在函数名前要加*号。 错误2:题目要求将小写字母改为大写字母,所以if语句的判断条件是小写字母。 相关知识 题目 编程题 请编写函数fun(),该函数的功能是:移动一维数组中的内容,若数组中有n个整数,要求把下标从p到n-1(p≤n-1)的数组元素平移到数组的前面。 例如,一维数组中的原始内容为1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,p的值为6。移动后,一维数组中的内容应为7,8,9,10,10,11,12,13,14,15,1,2,3,4,5,6。 试题程序: #include stdio.h #define N 80 void fun(int *w, int p, int n) { } main() { int a[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; int i, p, n=15; printf(The original data:\n); for(i=0;in;i++) printf(%3d,a[i]); printf(\n\nEnte

文档评论(0)

1亿VIP精品文档

相关文档