仿写标准库中的strlen、strcat、strcpy、strcmp函数,为了和标准库中的函数进行区分,自己编写的函数前添加m
仿写strlen函数
#include <stdio.h>
size_t mstelen(const char *str){
if(str == NULL){
return 0;
}
size_t ret = 0;
while(*str != '\0'){
str++;
ret++;
}
return ret;
}
仿写strcat函数
#include <stdio.h>
char * mstrcat(char *destination, const char *source){
if(destination == NULL){
return NULL;
}
if(source == NULL){
return destination;
}
char *ret = destination;
while(*destination != '\0'){
destination++;
}
while(*source != '\0'){
*destination = *source;
destination++;
source++;
}
*destination = '\0';
return ret;
}
仿写strcpy
#include <stdio.h>
char * mstrcat(char *destination, const char *source){
if(destination == NULL){
return NULL;
}
if(source == NULL){
return destination;
}
char *ret = destination;
while(*source != '\0'){
*destination = *source;
destination++;
source++;
}
*destination = '\0';
return ret;
}
仿写strcmp函数
#include <stdio.h>
int 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 == '\0' && *str2 == '\0'){
break;
}
str1++;
str2++;
}
}
return ret;
}