C语言|【c语言strcpy、strcat、strlen函数实现】

c语言strcpy、strcat、strlen函数实现

#include //实现strlen函数 int Mystrlen(char *str) { int len = 0; while(*str != '\0') { str++; len++; } return len; }//实现strcpy函数 void Mystrcpy(char *des,const char *src) { if(NULL == des) { return ; } while(*src != '\0') { *des++ = *src++; } *des = '\0'; } //实现strcat char *Mystrcat(char *des,const char *src) { char *temp = NULL; temp = des; //保存目的字符串首地址 while(*des++); des--; //指向结束字符串 while(*des++ = *src++); //进行循环赋值 *des = '\0'; return temp; } int main(void) { int result = 0; char *str = "hello"; char *str2 ="world"; result = Mystrlen(str); printf("result = %d\n",result); //测试二 char des[24]= {0}; Mystrcpy(des,str); printf("des = %s\n",des); //测试三 char *res = (char *)malloc(24); res =Mystrcat(Mystrcat(res,str),str2); printf("str = %s\n",res); return 0; }

    推荐阅读