前期提醒:这是比特鹏哥上课讲的内容,鹏哥讲的非常好,大家可以了解一下。
先来讲一讲strtok函数:
int main()
{
const char* p = "@.#";
char arr[] = "zpengwei@yeah.net#hehe";
char buf[50] = { 0 };// "zpengwei@yeah.net"
strcpy(buf, arr);
char* str = NULL;
for (str = strtok(buf, p); str != NULL; str=strtok(NULL, p))
{
printf("%s\n", str);
}//运用循环来实现
char* str = strtok(buf, p);//zpengwei
printf("%s\n", str);
str = strtok(NULL, p);//yeah
printf("%s\n", str);
str = strtok(NULL, p);//net
printf("%s\n", str);
strtok - 开始返回NULL
return 0;
}
实现memove和memcpy函数:
memcpy函数挺简单的,主要还是说一下memmove函数:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<assert.h>
void* my_memmove(void* dest,const void* str,size_t num)
{
void* ret = dest;
if (dest < str)//前->后
{
while (num--)
{
*(char*) dest = *(char*)str;
dest = (char*)dest + 1;
str = (char*)str + 1;
}
}
else//后->前
{
dest = (char*)dest + num-1;
str = (char*)str + num-1;
while (num--)
{
*(char*)dest = *(char*)str;
dest = (char*)dest - 1;
str = (char*)str - 1;
}
}
return ret;
}
void* my_memcpy(void* dest, const void* str, size_t num)
{
void* ret = dest;
assert(dest && str);
while (num--)
{
*(char*)dest = *(char*)str;
dest = (char*)dest + 1;
str = (char*)str + 1;
}
return ret;
}
int main(void)
{
int a[] = { 1,2,3,4,5,6,7,8,9,10 };
int b[10] = { 0 };
/*my_memmove(a+2,a,5*sizeof(int));*/
my_memcpy(b,a,5*sizeof(int));
int i = 0;
for (i = 0; i < 5; i++)
{
printf("%d ", b[i]);
}
return 0;
}