C考试程序设计题目,功能字符串连接,根据答案写程序,编译运行得不到想要的结果
程序代码:
# include <stdio.h> void fun (char p1[],char p2[]) { int i,j; for (i=0;p1[i]!='\0';i++) for(j=0;p2[j]!='\0'; ) p1[i++]=p2[j++]; p1[i]='\0'; } main() { char s1[80],s2[40]; //void NONO(); printf("Enter s1 and s2:\n"); scanf("%s%s",s1,s2); printf("s1=%s\n",s1); printf("s2=%s\n",s2); printf("Invoke fun(s1,s2)\n"); fun(s1,s2); printf("After invoking:\n"); printf("%s\n",s1); // NONO(); return 0; } /*void NONO() { int i; FILE *rf,*wf; char s1[80],s2[40]; rf = fopen("in.dat","r"); wf = fopen("out.dat","r"); for(i=0;i<10;i++) { fscanf(rf,"%s",s1); fscanf(rf,"%s",s2); fprintf(wf,"%s\n",s1); } fclose(rf); fclose(wf); }*/
运行结果
Enter s1 and s2:
a!2
b!c
s1=a!2
s2=b!c
Invoke fun(s1,s2)
After invoking:
b!c
Press any key to continue