码迷,mamicode.com
首页 > 其他好文 > 详细

实现strcat功能

时间:2016-02-29 10:48:12      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

实现两个字符串相连


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 }

 

实现strcat功能

标签:

原文地址:http://www.cnblogs.com/eeexu123/p/5226573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!