C语言:下面两题咋编程?
2、编写函数con(char s1[ ], char s2[ ])实现两个字符串的连接(不得使用库函数strcat), 即把字符数组s2中的字符串连接到字符数组s1字符串的后面。 例如main函数中输入"Good"和“Morning”,则在主函数中输出"GoodMorning" 展开
(1)
代码文本:
#include "stdio.h"
#define N 10
void sort(int a[],int n){
int i,j,k;
for(n-=2,i=3;i<n;i++)
for(j=3;j<n;j++)
if(a[j]>a[j+1])
k=a[j],a[j]=a[j+1],a[j+1]=k;
}
int main(int argc,char *argv[]){
int a[10],i;
printf("Enter %d integers...\n",N);
for(i=0;i<N;scanf("%d",a+i++));//输入N个整数
sort(a,N);//调用排序函数
puts("Operating results are as follows:");
for(i=0;i<N;printf("%d ",a[i++]));
putchar('\n');
return 0;
}
//////////////////////////
(2)
代码文本:
#include "stdio.h"
char *con(char s1[],char s2[]){
char *t=s1;
while(*s1)
s1++;
while(*s1++=*s2++);
return t;
}
int main(int argc,char *argv[]){
char s1[100],s2[50];
printf("Enter 2 strings...\n");
scanf("%49s%49s",s1,s2);
printf("\nThe result is:\n%s\n",con(s1,s2));
return 0;
}
2022-12-05 广告
#include "stdio.h"
void sort(int a[],int n)
{ int i,j,t;
for(i=0; i<n-1; i++)
for(j=0; j<n-1-i; j++)
if(a[j]>a[j+1])
{ t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
int main()
{ int n,i,a[500];
scanf("%d",&n);
for(i=0; i<n; i++)
scanf("%d",&a[i]);
sort(a+3,6);
for(i=0; i<n; i++)
printf("%d ",a[i]);
return 0;
}
=======================
#include "stdio.h"
void con(char s1[ ], char s2[ ])
{ int i,j;
for(i=0; s1[i]; i++);
for(j=0; s1[i++]=s2[j++];);
}
int main()
{ char s1[20]="Good",s2[]="Morning";
con(s1,s2);
printf("%s\n",s1);
return 0;
}