仿写标准库中的strlen、strcat、strcpy、strcmp函数,为了和标准库中的函数进行区分,自己编写的函数前添加m
仿写strlen函数
#includesize_t mstelen(const char *str){ if(str == NULL){ return 0; } size_t ret = 0; while(*str != ' '){ str++; ret++; } return ret; }
仿写strcat函数
#includechar * mstrcat(char *destination, const char *source){ if(destination == NULL){ return NULL; } if(source == NULL){ return destination; } char *ret = destination; while(*destination != ' '){ destination++; } while(*source != ' '){ *destination = *source; destination++; source++; } *destination = ' '; return ret; }
仿写strcpy
#includechar * mstrcat(char *destination, const char *source){ if(destination == NULL){ return NULL; } if(source == NULL){ return destination; } char *ret = destination; while(*source != ' '){ *destination = *source; destination++; source++; } *destination = ' '; return ret; }
仿写strcmp函数
#includeint mstrcmp(const char *str1, const char *str2){ if(str1 == NULL && str2 == NULL){ return 0; } if(str1 == NULL && str2 != NULL){ return 1; } if(str1 != NULL && str2 == NULL){ return -1; } int ret = 0; while(1):{ if(*str1 != *str2){ if(*str1 > *str2){ ret = 1; }else{ ret = -1; } break; } else { if(*str1 == ' ' && *str2 == ' '){ break; } str1++; str2++; } } return ret; }



