给出一个函数来合并两个字符串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;
}
#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;
}
相关文章推荐
- 给出一个函数来合并两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠
- 给出一个函数来合并两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠
- 给出一个函数来合并两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠
- 4、给出一个函数复制两个字符串A和B,A的后几个和B的前几个字节重叠
- C++ - 给出一个函数来连接两个字符串A和B,其中字符串A的后几个字节和字符串B的前几个字节重叠
- 请编写实现malloc()内存分配函数功能一样的代码。给出一个函数来复制两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠。
- 给出一个函数,来复制两个字符串A,B ,字符串A的后几个字节和字符串B的前几个字节重叠
- 给出一个函数来合并两个字符串A和B
- 合并两个字符集合为一个新集合,每个字符串在新集合中仅出现一次,函数返回新集合中字符串。
- 合并两个字符串A、B,A的后几个字节同B的前几个字节
- 85 复制两个字符串 A 和 B。字符串A的后几个字节和字符串B的前几个字节重叠
- 合并两个字符集合为一个新集合,每个字符串在新集合中仅出现一次,函数返回新集合中字符串。
- 剑指offer2.3.2字符串:有两个排序的数组A1和A2,内存在A1的末尾有足够多的空余空间容纳A2,编写一个函数,将A2合并到A1并排序
- 合并两个字符集合为一个新集合,每个字符串在新集合中仅出现一次,函数返回新集合中字符串。 如: s1集合{“while”,”for”,”switch”,”if”,”continue”
- 利用字符串和列表将两个通讯录文本合并为一个文本
- php中按字节截取字符串方法,(汉字占两个字节,字母占一个字节,页面编码必须为utf-8)
- 10、 java 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 要求不能出现截半的情况
- P279_1017 写一函数,实现两个字符串的比较,即自己写一个strcmp函数
- 1.4 写一个函数判断两个字符串是否使用相同的字符构成。
- oracle数据库中同一个表的两个字段 数据合并成一个数据以及字符串补0