#include
//检查每一个是否为0
size_t my_strlen(const char a[])
{
int i = 0;
for (i = 0; a[i] != '\0'; i++)
{
;
}
return i;
}
//指针-指针
size_t my_strlen2(char *a)
{
char* b = a;
while (*b != '\0')
{
b++;
}
return b - a;
}
//递归,不创建新的变量
size_t my_strlen3(char *a)
{
if (*a != '\0')
{
return 1 + my_strlen(a + 1);
}
else
return 0;
}
int main()
{
char a[100] = "abcdefg";
printf("%d\n", my_strlen(a));
printf("%d\n", my_strlen2(a));
printf("%d\n", my_strlen3(a));
}
结果:
#include
#include
#include
#include
char* my_strcpy(const char* a, char* b)
{
assert(a && b);
char* c = b;
while (*a)
{
*b++ = *a++;
}
return c;
}
int main()
{
char a[100] = "abcdefg";
char b[200] = "xxxxxxxxxxxxxxx";
printf("%s\n", my_strcpy(a, b));
}
结果:
#include
#include
#include
#include
char* my_strcat( char* a, const char* b)
{
assert(a && b);
char* c = a;
//1.找到目标字符串的末尾
while (*c)
{
c++;
}
//2.追加原字符串,直到\0
while (*c++ = *b++)
{
;
}
return a;
}
int main()
{
char a[200] = "我写代码";
char b[200] = "感到很有趣";
printf("%s\n", my_strcat(a, b));
return 0;
}
结果:
int strcmp ( const char * str1, const char * str2 );
#include
#include
#include
#include
int my_strcmp(const char* a, const char* b)
{
while (*a == *b)//先判断每一个字符是否相同
{
if (*a == '\0')//相同的话,再判断是否都为\0
{
return 0; //都为\0的话,则说明两字符串相同
}
a++;
b++;
}
return *a - *b; //返回不相同的字符ASCII码相减后的值
}
int main()
{
char a[200] = "aafg";
char b[200] = "aabc";
int i = my_strcmp(a, b);
if (i > 0)
{
printf("a大\n");
}
else if (i == 0)
{
printf("相同\n");
}
else
printf("b大\n");
return 0;
}
结果:
char * strstr ( const char *, const char * );
#include
#include
#include
#include
char* my_strstr(char* str1,char* str2)
{
assert(str1 && str2);
char* s1;
char* s2;
char* cp = str1;
if (*str2 == '\0')
{
return str1;
}
while (*cp)
{
s1 = cp;
s2 = str2;
while (*s1 && *s2 && *s1 == *s2)
{
s1++;//从a的每一个字符开始匹配,并记录匹配起始点
s2++;
}
if (*s2 == '\0')
{
return cp;
}
cp++;//不相同,从下一个字符开始重新匹配
}
//*cp指向a的‘\0’了,依旧找不到
return NULL;
}
int main()
{
char arr1[] = "i am a student, hello!";
char arr2[] = "student";
//查找arr1中arr2第一次出现的位置
char *ret = my_strstr(arr1, arr2);
if (ret == NULL)
{
printf("找不到\n");
}
else
{
printf("%s\n", ret);
}
return 0;
}
结果:
void * memcpy ( void * destination, const void * source, size_t num );
#include
#include
#include
#include
void* my_memcpy(void* a, const void* b, int sz)
{
assert(a&&b);
void *ret=a;
while (sz--)
{
*(char*)a = *(char*)b;
a = (char*)a + 1;
b = (char*)b + 1;
}
return ret;
}
int main()
{
int a[] = { 0,1,2,3,4,5 };
int b[15] = { 0 };
my_memcpy(b,a,6*sizeof(int));
for (int i = 0; i < 15; i++)
{
printf("%d ", b[i]);
}
return 0;
}
结果:
void * memmove ( void * destination, const void * source, size_t num );
#include
void* my_memmove(void* a, const void* b,int sz )
{
void* ret = a;
if (a < b)//从前向后
{
while (sz--)
{
*(char*)a = *(char*)b;
a= (char*)a+1;
b= (char*)b+1;
}
}
else//从后向前
{
while (sz--)
{
*((char*)a+sz) = *((char*)b+sz);
}
}
return a;
}
int main()
{
int a[] = { 1,2,3,4,5,6,7,8,9,10 };
my_memmove(a+2,a,20);
for (int i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
return 0;
}
结果:
如果这篇文章对你有所帮助,请点一个赞支持一下吧~欢迎各位留下理性的评论,与博主友好交流!