有关strcpy与strlen的实现

来源:互联网 发布:dts播放器 for mac 编辑:程序博客网 时间:2024/06/04 19:22
#define _CRT_SECURE_NO_WARNINGS 1    模拟实现strcpy
#include<stdio.h>
#include<assert.h>
#include<string.h>
char*my_strcpy(char*pdest,const char*psou)//*形成链式访问
{
char*ret = pdest;
assert(pdest != NULL);
assert(psou != NULL);
while (*pdest++ = *psou++)//后置++,先使用在+1
{
;
}
return ret;
}
int main()
{
char arr[20];
char*p= "Hello bit.";
my_strcpy(arr, "Hello bit.");
printf("%s",arr);
getchar();
return 0;

}



模拟实现strlen


#include<stdio.h>
#include<assert.h>
#include<string.h>


size_t my_strlen(const char*str)
{
int count = 0;
assert(str != NULL);
while (*str != '\0')
{
str++;
count++;
}
return count;
}
int main()
{
char *p = "abcdef";
my_strlen(p);
int ret = 0;
ret = my_strlen(p);
printf("%d\n",ret);
system("pause");
}


0 0
原创粉丝点击