#include
#include
typedef unsigned int size_t;
typedef char u8;
size_t my_strlen(const char *str)
{
size_t len = 0;
while (*str)
{
len++;
str++;
}
return len;
}
char *my_strcpy(char *pdst, const char *psrc)
{
printf("@%s()\n", __func__);
while ((*pdst++ = *psrc++));
return pdst;
}
int my_strcmp(const char *s1, const char *s2)
{
printf("@%s()\n", __func__);
while (*s1 == *s2)
{
if ('\0' == *s1)
{
return 0;
}
s1++;
s2++;
}
return *s1 - *s2;
}
void *my_memcpy(void *pdst, const void *psrc, size_t num)
{
u8 *pd;
const u8 *ps;
pd = pdst;
ps = psrc;
printf("@%s()\n", __func__);
while (num > 0)
{
//*(char *)pdst++ = *ps++;
*pd++ = *ps++;
num--;
}
return pdst;
}
void *my_memset(void *buff, int c, size_t size_num)
{
size_t i;
char *ps = buff;
printf("@%s()\n", __func__);
for (i = 0; i < size_num; i++)
{
((char *)buff)[i] = c;
//*ps = c;
//ps++;
}
return buff;
}
int main(int argc, char *argv[])
{
const char *str = "hello";
char *cpto;
size_t len;
int i;
const char *str1 = "abc";
const char *str2 = "acb";
//memcpy
len = 3;
cpto = (char *)malloc(len);
my_memcpy(cpto, str, len);
printf("str = %s\n", cpto);
//strcpy
cpto = (char *)malloc(my_strlen(str));
my_strcpy(cpto, str);
printf("str = %s\n", cpto);
//strcmp
i = my_strcmp(str1, str2);
printf("The compare result is : %d\n", i);
//memset
//cpto = (unsigned char *)malloc(len * sizeof(int));
my_memset(cpto, 127, len);
for (i = 0; i < len; i++)
{
printf("%d\n", cpto[i]);
}
return 0;
}
阅读(873) | 评论(2) | 转发(0) |