分别模拟实现库函数
1.strcat
2.strcmp
3.strcpy
4.srelen
1.strcat
#include
#include
void Merge(char a[], char b[], int n)
{
char* q = a;
char* p = b;
int m = 0;
while (*q != '\0')
q++;
while (m <= n) {
*q++ = *p++;
m++;
}
puts(a);
}
int main()
{
char a[100] = { 0 };
char b[100] = { 0 };
gets(a);
gets(b);
int n = strlen(b);
Merge(a, b, n);
}
2.strcmp
#include
#include
int Strcmp(const char* str1, const char* str2) {
assert(str1 != NULL);
assert(str2 != NULL);
while (*str1 == *str2) {
if (*str1 == '\0') {
return 0;
}
++str1;
++str2;
}
return *str1 - *str2;
}
int main() {
char str1[] = "love";
char str2[] = "move";
int point = Strcmp(str1, str2);
if (point == 0) {
printf("str1 = str2\n");
}
else if (point > 0) {
printf("str1 > str2\n");
}
else {
printf("str1 < str2\n");
}
return 0;
}
3.strcpy
#include
#include
void my_strcpy(char *arr_1, char *arr_2)
{
while (*arr_2!=0) {
*arr_1++ = *arr_2++;
}
}
int main()
{
char arr_1[100] = { 0 };
char arr_2[100] = { 0 };
gets_s(arr_1);
/*int a = strlen(arr_1);*/
gets_s(arr_2);
my_strcpy(arr_1, arr_2);
printf("%s", arr_1);
}
4.srelen
#include
void my_strlen(char* arr)
{
int count = 0;
while (*arr != 0) {
arr++;
count++;
}
printf("%d", count);
}
int main()
{
char arr[100] = { 0 };
gets_s(arr);
my_strlen(arr);
}
来源:51CTO
作者:LHlucky2
链接:https://blog.51cto.com/14946388/2551657