strlen函数模拟
size_t my_strlen(const char* arr) {int count = 0;while(*arr){arr++;count++;}return count;}
int main()
{
printf( " %zd", my_strlen("adsshadsa"));}//模拟实现strlen函数
strcpy函数模拟
char* my_strcpy(char* arr1, const char* arr2){char* arr3 = arr1;while (*arr1 = *arr2){arr1++;arr2++;}return arr3;}int main(){char arr1[] = "adsddassd";my_strcpy(arr1, "sahdhd");printf("%s", arr1);
}//模拟strcpy函数
模拟strcmp函数
int my_strcmp(const char* arr1, const char* arr2)
{while (*arr1 == *arr2) {if (!*arr1)return 0;arr1++;arr2++;}return *arr1 - *arr2;
}
int main()
{int ret = my_strcmp("dasdsa", "dasdst");printf("%d", ret);}//模拟strcmp函数
模拟strcat函数
char* my_strcat(char* arr1, const char* arr2){char* arr3 = arr1;while (*arr1)arr1++;while (*arr1 = *arr2){arr1++;arr2++;}return arr3;}int main(){char arr1[30] = "adsddassd";my_strcat(arr1, "sahdhd");printf("%s", arr1);
}//模拟strcpy函数
长度限制字符串函数就不模拟了
这里我们学的长度限制字符串函数就不模拟了,如strncat,strncpy,strncmp。其代码内容相比原本无限制的就多了几个限制条件,这里就不模拟了
strstr模拟
char* my_strstr(const char* arr1, const char* arr2)
{if (!*arr2)return (char*)arr1;const char* arr3;const char* arr4;while (*arr1){arr3 = arr1;arr4 = arr2;while (*arr3 == *arr4){arr3++;arr4++;if (*arr4 == 0)return (char*)arr1;}arr1++;}return NULL;
}int main()
{
char* ret= my_strstr("adsdasdf", "das");
printf("%s", ret);}//模拟strstr函数