编写一个程序,采用一个函数实现字符串的前后连接和后前连接。怎么做!

来源:百度知道 编辑:UC知道 时间:2024/06/30 21:19:32
如题所述,怎么做!!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 80
int main()
{
char *stick(char str1[],char str2[]);
char a[SIZE],b[SIZE];
char *p,*q;
printf("输入字符串a:\n");
gets(a);
printf("输入字符串b:\n");
gets(b);
p=stick(a,b); //指针p指向前后连接后的数组
q=stick(b,a); //指针q指向后前连接后的数组
printf("字符串的前后连接为:%s\n",p);
printf("字符串的前后连接为:%s",q);
return 0;
}

char *stick(char str1[],char str2[])
{
int i=0,j;
char *temp; // 让字符指针指向新开辟的一段空间
temp=(char *)malloc(strlen(str1)+strlen(str2)+1);
for(j=0;str1[j]!='\0';j++) //数组a复制进temp数组
{
temp[i++]=str1[j];
}
for (j=0;str2[j]!='\0';j++) //数组b复制进temp数组
{
temp[i++]=str2[j];
}
temp[i]=