实现strcat功能

实现两个字符串相连


1
#include<stdio.h> 2 #include<string.h> 3 4 //把源字符串连接到目的字符串中 5 char *strcat1(char *dest,const char *src) 6 { 7 int i=0; 8 int n=strlen(dest); 9 10 if((dest != NULL) && (src != NULL)) //源指针和目的指针不能为NULL 11 { 12 for(i=0;src[i] != '\0';i++) 13 { 14 dest[n+i] = src[i]; 15 } 16 dest[n+i] = '\0'; 17 } 18 19 return dest; 20 } 21 22 //从源字符串中,连几个字符到目的字符串中 23 char *strcat2(char *dest,const char *src,int num) 24 { 25 int i=0; 26 int n= strlen(dest); 27 28 if((dest != NULL) && (src != NULL)) //源指针和目的指针不能为NULL 29 { 30 for(i=0;i<num && src[i] != '\0';i++) 31 { 32 dest[n+i] = src[i]; 33 } 34 dest[n+i] = '\0'; 35 } 36 37 return dest; 38 } 39 40 41 int main() 42 { 43 char a[20]="hello "; 44 char *p="world!123"; 45 //测试strcat1函数 46 strcat1(a,p); 47 printf("After strcat1 function,a=%s\n",a); 48 49 //测试strcat2函数 50 char b[20]="beautiful "; 51 strcat2(b,p,6); 52 printf("After strcat2 function,b=%s\n",b); 53 54 return 0; 55 }

 

转载于:https://www.cnblogs.com/eeexu123/p/5226573.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值