——Rex's Blog,Westlife歌迷,Linuxer
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char a[100],b[100],*c; int i,j,k=0,l=0,lena,lenb,lenc; printf("Please input the first word:\n"); gets(a); printf("Please input the second word:\n"); gets(b); lena=strlen(a); lenb=strlen(b); c=(char *)malloc((lena+lenb-1)*sizeof(char)); //向内存申请适合的空间 lenc=strlen(c); for (j=0;j<=lena-1;j++) { c[k]=a[j]; k++; } for (i=lena;i<lenc;i++) { c[i]=b[l]; l++; } puts(c); return 0; }
或者可以使用
int lena = strlen(stra); int lenb = strlen(strb); for(int i = 0; i < lenb; i++) stra[lena + i] = stra[i]; stra[lena + lenb] = '\0';
以下为自写函数达到同样效果
#include <stdio.h> #include <string.h> #include <stdlib.h> void lianjie(char a[],char b[]) { char *c; int i,j,k=0,l=0,lena,lenb,lenc; lena=strlen(a); lenb=strlen(b); c=(char *)malloc((lena+lenb-1)*sizeof(char)); lenc=strlen(c); for (j=0;j<=lena-1;j++) { c[k]=a[j]; k++; } for (i=lena;i<lenc;i++) { c[i]=b[l]; l++; } puts(c); } int main() { char a[100],b[100]; printf("Please input the first word:\n"); gets(a); printf("Please input the second word:\n"); gets(b); lianjie(a,b); return 0; }
转载请注明:Living Beyond The Clouds 仿若云端 [http://blog.rex.la]
本文链接地址:http://blog.rex.la/1035.html
alipay
Dreamhost
Http Error
Linux
Nginx
Opera
Our Story
PHP
The Love Album
WHMCS
WinRAR
Wordpress
三坊七巷
天使
如果云知道
思念
搬家
教程
杂文
漏洞
笔记
自定义PHP.INI
花开的诺言
随想
青春 C (9)
Nginx (4)
Our Story (27)
The Love Album (13)
教程 (19)
杂文 (7)
随想 (24)
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
回复“C语言笔记:不使用Strcat函数连接两个字符串”
我要留言