1. 程式人生 > >字串內建函式的實現

字串內建函式的實現

 1 char *MyStrcpy(char *des,const char *src)
 2 {
 3 assert(des != NULL && src != NULL);
 4 char *pstr = des;
 5 while((*pstr++ = *src++)!= '\0');
 6 return des;
 7 }
 8 
 9 char *MyStrcat(char *des,const char *src)
10 {
11 assert(des != NULL && src!= NULL);
12 char *pdes = des;
13 while
(*pdes != '\0') ++pdes; 14 while((*pdes++ = *src++) != '\0'); 15 return des; 16 } 17 18 void *MyMemcpy(void *des,const void *src,unsigned int count) 19 { 20 assert(des !=NULL && src != NULL && count>0); 21 char *pdes = (char *)des; 22 int i=0; 23 while(i<count && *(char *)src != '
\0') 24 { 25 *pdes++=*(char *)src; 26 src=(char *)src+1; 27 ++i; 28 } 29 *pdes = *(char*)src; 30 return des; 31 } 32 33 void* MyMemmove(void *des,const void *src,unsigned int count) 34 { 35 assert(des != NULL && src != NULL && count >0); 36 char *pdes = (char *)des; 37 if(des < src || (char
*)des >(char *)src + count) 38 { 39 int i = 0; 40 while(i<count && *(char *)src != '\0') 41 { 42 *pdes++ = *(char *)src; 43 src = (char *)src+1; 44 ++i; 45 } 46 } 47 else 48 { 49 pdes = pdes + count; 50 src = (char *)src + count -1; 51 *pdes-- = '\0'; 52 int i= count -1; 53 while(i>=0) 54 { 55 *pdes--=*(char *)src; 56 i--; 57 src = (char *)src -1 ; 58 } 59 } 60 return des; 61 }