您的位置:首页 > 其它

给出一个函数来合并两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠

2013-08-02 16:26 381 查看
我的这个算法的比较直观通俗易懂,欢迎大家提出建议和意见哈

#include <stdio.h>
#include <stdlib.h>

char * connect(char *a,char *b){
int lenA=strlen(a);
int lenB=strlen(b);
int count=0;
char *second;
char *first=a;
while(*first!='\0'){
second=b;
if(*first==*second){
while(*first==*second){
first++;
second++;
count++;
}
if(*first=='\0'){
break;
}else{
count=0;
}
}else{
first++;
}
}

printf("count:%d\n",count);//主要是找到究竟有多少个字符重复
char *result=(char*)malloc(lenA+lenB-count);
a[lenA-count]='\0';
strcpy(&a[lenA-count],b);
strcpy(result,a);
return result;
}

int main()
{
char a[]="hfhhhqiaqiaou";
char b[]="qiaouernmcvsdwe";
printf("before connect the two string is\n%s\n%s\n",a,b);
printf("after connect is %s\n", connect(a,b));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐