一,模拟strlen函数
#include<stdio.h>
int my_strlen(char *ar)
{
int i = 0;
while (*ar != '\0')
{
i++;
*ar++;
}
return i;
}
int main()
{
char arr[] = "abcdefg";
int x = my_strlen(arr);
printf("%d", x);
return 0;
}
二,模拟strcat
#include<stdio.h>
char* my_strcat(char* d, char* s)
{
char* i = d;
while (*d)
{
d++;
}
while (*d++ = *s++)
{
;
}
return i;
}
int main()
{
char arr1[20] = "1234567";
char arr2[] = "hollo";
my_strcat(arr1, arr2);
printf("%s", arr1);
return 0;
}
三,模拟strcpy
#include<stdio.h>
char* my_strcpy(char* d, const char* s)
{
char* a = d;
while (*d++ = *s++)
{
;
}
return a;
}
int main()
{
char arr1[] = "1234567";
char arr2[] = "abcdefg";
my_strcpy(arr1, arr2);
printf("%s", arr1);
return 0;
}
四,模拟strstr
#include<stdio.h>
char* my_strstr(char*d,char*s)
{
char* s1 = d;
char* s2 = s;
char* c = d;
while (*c )
{
s1 = c;
s2 = s;
while (*s1 && *s2 && (*s1 == *s2))
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return c;
}
c++;
}
return NULL;
}
int main()
{
char arr1[] = "123456";
char arr2[] = "56";
char* p = my_strstr(arr1, arr2);
if (p==NULL)
{
printf("找不到了");
}
else
{
printf("%s\n", p);
}
return 0;
}