{ char str1[20], str2[10];
int i=0,j=0; gets(str1); gets(str2)?
while(str1[i]!=?\\0?) i++;
while(str2[j]!=?\\0?) str1[i++]=str2[j++]; str1[i]=?\\0?; puts(str1);
}
2. # define M 3
# define N 4
# include 3. # include 习题参考答案 305 习题参考答案 { int i, j, a[5][5]={{2,8,1,9,4},{5,7,1,3,0},{7,1,7,5,2},{3,2,2,1,5},{0,2,1,6,8}}; for (i=0;i<5;i++) { for (j=0;j<5;j++) printf(“M”, a[i][j]); printf(“\\n”); find(a); } 4. #include { int a[3][5],x,y,i,j,k,flag=0; int max,min; for(i=0;i<3;i++) for(j=0;j<5;j++) scanf(\ /**/ for(i=0;i<3;i++) { max=a[i][0];y=0; for(k=1;k<5;k++) if(max for(k=0;k<3;k++) if(min>a[k][y]) {x=k;min=a[k][y];} if(i==x) {printf(\} if(flag==0) printf(\} 5. # include { a[j]=a[j+1]; j++; } a[N-1]=temp; } main() { int a[N]={8,5,6,9,4,1,-1,7,3,2}; int i; crl(a); for (i=0; i 6. # include # include int Replace_string(char line[ ], char str1[ ], char str2[ ]) { int i=0, j, loc; char temp[80]; while(i<=strlen(line)-strlen(str2)) { j=0; loc=i; while(str1[j]= =line[loc]&&str1[j]!=?\\0?) { loc++; j++; } if (str1[j]= =?\\0?) { strcpy(temp, &line[loc]); strcpy(&line[i], str2); i+=strlen(str2); strcpy(&line[i], temp); return 1; } else i++; } return 0; } 习 题 7 一、选择题 1.A 2.A 3. D 4.C 5. A 6.C 7.B 8.C 9.B 10.D 二、填空题 1.a=0,b=7 2.110 3.20 习题参考答案 307 习题参考答案 4.abc,ABC,bc,BC,c,C, 5.1 4 6. len++ p++ 三、编程题 1、编写一个程序,将字符串computer赋给一个字符数组,然后从第一个字母开始间隔地输出该字符串,请用指针完成。 main() {char ch[20]=\ int i; for(i=0;i 2、输入3个字符串,按由小到大的顺序输出。 main() {char str1[20],str2[20],str3[20]; char char p[20],*p1,*p2; printf(\ gets(str1); gets(str2); gets(str3); p1=str1;p2=str2; if(strcmp(str1,str2)>0) { strcpy(p,p1);strcpy(p1,p2); strcpy(p2,p); } p1=str1;p2=str3; if(strcmp(str1,str3)>0) { strcpy(p,p1);strcpy(p1,p2); strcpy(p2,p); } p1=str2;p2=str3; if(strcmp(str2,str3)>0) { strcpy(p,p1);strcpy(p1,p2); strcpy(p2,p); } printf(\ printf(\} 3、求一个3×3矩阵主对角线元素之和。 main() {int a[3][3],*p,i,s=0; for(i=0;i<3;i++) scanf(\ p=&a[0][0]; for(i=0;i<3;i++) s=s+*(p+3*i+i); 308 习题参考答案 printf(\} 4、将一个3×3矩阵转置。 main() {int a[3][3],*p; int i,j,t; printf(\ for(i=0;i<3;i++) scanf(\ p=&a[0][0]; for(i=0;i<3;i++) for(j=i;j<3;j++) {t=*(p+3*i+j); *(p+3*i+j)=*(p+3*j+i); *(p+3*j+i)=t; } printf(\ for(i=0;i<3;i++) printf(\} 5、输出给定的字符串中某一个字符前面的一串字符。如指定字符'a',则对字符串\,输出\。若无指定字符,则输出相应的提示信息。 #include char *ptr,str[10],ch; printf(\ scanf(\ ptr=str; getchar(); printf(\ scanf(\ while(ch=='\\n') {printf(\ scanf(\ } while(*ptr!=ch) {ptr++;n++;} 309