这篇是如何使用库函数之少写点代码哈哈哈,为啥有库函数呢?,应为这个大家都用,且,频率还高,每次用还要自己实现,麻烦所以就诞生了库函数,方便
char* des2 = des;
/*while (*con&&*des)
{
*des = *con;
des++;
con++;
}*/
while (*des++=*con++)
{
;
}
return des2;
}
int main()
{
char arr1[20] = "ABCDE";
char arr2[] = "FGHIJ";
printf("%s",my_strcpy(arr1, arr2));
return 0;
}
while (限制条件--)
{
*des = *con;
des++;
con++;
}
#include
int my_strcmp(const char *str1,const char* str2)
{
assert(str1&&str2);
while (*str1 == *str2&& *str1&& *str2)
{
str1++;
str2++;
}
if (*str1 - *str2== 0)
{
return 0;
}
else
{
return *str1 - *str2;
}
}
int main()
{
char arr1[] = "ABCDE";
char arr2[] = "CDEBB";
int ret1 = strlen(arr1);
int ret2 = strlen(arr2);
if (ret1 == ret2)
{
int ret = my_strcmp(arr1, arr2);
if (ret != 0)
{
printf("不等于\n");
}
else
{
printf("等于");
}
}
else
{
printf("不相等\n");
}
}
实现只需要传参的时候多一个限条件,在写个循环条件是限制条件
int my_strcmp(const char *str1,const char* str2,int 限制条件)
{
assert(str1&&str2);
while(x)
while (*str1 == *str2&& *str1&& *str2)
{
str1++;
str2++;
}
if (*str1 - *str2== 0)
{
return 0;
}
else
{
return *str1 - *str2;
}
}
char* my_strcat(char* des,const char* sour)
{
assert(des && sour);
char* des2 = des;
while (*des!='\0')
{
des++;
}
while (*des!=*sour)
{
*des++ = *sour++;
}
return des2;
}
int main()
{
char arr1[20] = "hello \0########";
char arr2[] = "world";
//strcat(arr1, arr2);
printf("%s",my_strcat(arr1, arr2));
return 0;
}
char* my_strstr(const char *dest,const char *cour)
{
assert(dest && cour);
const char* dest2 = dest;
const char* cour2 = cour;
if (*cour == '\0')
{
return (char*)dest;
}
while (*dest2)
{
dest = dest2;
cour2 =cour ;
while (( *dest == *cour2)&&*dest&&*cour2)
{
dest++;
cour++;
}
if(*dest==*cour&&*dest!='\0')
return (char*)dest2;
dest2++;
}
return NULL;
}
int main()
{
char arr1[] = "ABBC";
char arr2[] = "CCC";
char *ret =my_strstr(arr1,arr2);
if (ret != NULL)
{
printf("是该字符的子串");
}
else
{
printf("不是该字符的子串");
}
return 0;
}
int main() {
FILE* pFile;
pFile = fopen("unexist.ent", "r");
if (pFile == NULL)
printf("该错误是: %s\n", strerror(errno));
//errno: Last error number
return 0;
}
应为我在文件中就没有创建这个文件
#include
#include
#include
//void* my_memcpy(void* dest, const void* sour, size_t sz)
//{
// assert(dest&&sour);
// void* destm = dest;
// while (sz--)
// {
// *(char*)dest = *(char*)sour;
// dest=(char*)dest + 1;
// sour=(char*)sour + 1;
//
// }
// return destm;
//}
void* my_memcpy(void* dest, const void* sour, size_t sz)
{
assert(dest && sour);
void* destm = dest;//返回值
char* destt = (char*)dest;
char* sourr = (char*)sour;
while (sz--)
{
*destt++ = *sourr++;
}
return destm;
}
int main()
{
int arr1[10] = { 1,2,3,4,5 };
int arr2[] = { 6,7,8,9,10 };
int sz = sizeof(arr2);
my_memcpy(arr1, arr2, 20);//内存拷贝
return 0;
}
这里需要注意的是强制类型转换和后置加加,强制类型转换他是零时的,你给他++他又没有那个性质了,就是类似
buff时间到
了的感觉,还有优先级考虑的话++的优先级比强制类型转换的高
void* my_memmove(void *dest,void *sour,size_t num)
{
assert(dest&&sour);
void* destm = dest;
char* destt = (char*)dest;
char* sourr = (char*)sour;
while (num--)
{
if (destt < sourr)//地址比较
{
*destt++ = *sourr++;
}
else if(destt >sourr)
{
*(destt + num) = *(sourr + num);//地址在减少的时候他们也在减少
}
else
{
*destt++ = *sourr++;
}
}
return destm;
}
void print(int arr[],int sz)
{
int i = 0;
for ( i = 0; i <sz; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8, };
int sz = sizeof(arr1) / sizeof(arr1[0]);//元素个数
my_memmove(arr1+3, arr1,20);
print(arr1,sz);
printf("\n");
my_memmove(arr1, arr1 + 3, 20);
print(arr1, sz);
printf("\n");
return 0;
}
如果有错误请直接提出,评论区,私信,qq(1696912943),误人子弟可不好
博主用的画图软件下载链接
持续更新中……………………