实现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] != '';i++) 13 { 14 dest[n+i] = src[i]; 15 } 16 dest[n+i] = ''; 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] != '';i++) 31 { 32 dest[n+i] = src[i]; 33 } 34 dest[n+i] = ''; 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 ",a); 48 49 //测试strcat2函数 50 char b[20]="beautiful "; 51 strcat2(b,p,6); 52 printf("After strcat2 function,b=%s ",b); 53 54 return 0; 55 }
原文地址:https://www.cnblogs.com/eeexu123/p/5226573.html