019
模拟实现strcpy()
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strcpy(char *dest, const char *src)
{
char *ret = dest;
assert(dest != NULL);
assert(src != NULL);
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char *str1 = "abcdef";
char str2[20] = { 0 };
printf("%s\n", my_strcpy(str2, str1));
system("pause");
return 0;
}
模拟实现strcat()
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strcat(char *dest, const char *src)
{
char *ret = dest;
assert(dest != NULL);
assert(src != NULL);
while (*dest!='\0')
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char str1[20] = "abcd";
const char *str2 = "efg";
printf("%s\n", my_strcat(str1, str2));
system("pause");
return 0;
}
模拟实现strstr()
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char * my_strstr(char *str, char *substr)
{
assert(str != NULL);
assert(substr != NULL);
char *s1 = str;
char *s2 = substr;
char *cur = str;
while (*cur)
{
s1 = cur;
s2 = substr;
while (*s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
return cur;
if (*s1 == '\0')
return NULL;
cur++;
}
return NULL;
}
int main()
{
char arr1[] = "abcabcdabcg";
char arr2[] = "abcd";
printf("%s\n", my_strstr(arr1, arr2));
system("pause");
return 0;
}
模拟实现strchr()
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char *my_strchr(const char *s, char c)
{
char *ret = NULL;
assert(s != NULL);
while (*s)
{
if (*s == c)
{
ret = (char *)s;
return ret;
}
s++;
}
return NULL;
}
int main()
{
char arr[] = "abcdef";
char a = 'g';
printf("%s\n", my_strchr(arr, a));
system("pause");
return 0;
}
模拟实现strcmp()
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
int my_strcmp(const char *str1, const char *str2)
{
assert(str&&str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
if ((*str1 - *str2) > 0)
return 1;
else
return -1;
}
int main()
{
char *str1 = "abaz";
char *str2 = "abaz";
int ret = my_strcmp(str1, str2);
if (ret == 0)
printf("str1==str2\n");
else if (ret > 0)
printf("str1>str2\n");
else
printf("str1<str2\n");
system("pause");
return 0;
}
模拟实现memcpy()
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
void * my_memcpy(void *str1, const void *str2, size_t num)
{
void *ret = str1;
unsigned int i = 0;
assert(str1&&str2);
for (i = 0; i < num; i++)
{
*(char *)str1 = *(char *)str2;
++(char *)str1;
++(char *)str2;
}
return ret;
}
int main()
{
int arr1[20] = { 1, 2, 3, 45 };
int arr2[20] = { 1, 5, 5, 5, 5, 5, 5 };
my_memcpy(arr1 + 2, arr2, 3 * sizeof(arr2[0]));
system("pause");
return 0;
}
模拟实现memmove()
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
void * my_memmove(void *str1, const void *str2, size_t num)
{
void *ret = str1;
assert(str1&&str2);
if (str1 < str2)
{
while (num--)
{
*(char *)str1 = *(char *)str2;
str1 = (char *)str1 + 1;
str2 = (char *)str2 + 1;
}
}
else
{
while (num--)
{
*((char *)str1 + num) = *((char *)str2 + num);
}
}
return ret;
}
int main()
{
int arr[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
my_memmove(arr + 3, arr + 5, 8 * sizeof(arr[0]));
system("pause");
return 0;
}
文章最后发布于: 2018-07-29 16:59:10
相关阅读
图片来源图虫:已授站长之家使用声明:本文来自于微信公众号运营研究社公众号(ID:U_quan),作者:陈维贤,授权站长之家转载发布。文章整理自
与E1000E和E1000相比,VMXNET3的网络性能更好。本文将解释虚拟网络适配器和第2部分之间的区别,并将演示通过选择半虚拟化适配器可以
我们生活中所说的真无线耳机其实就是两个“耳塞”,主体没有任何的可见线材,索尼真无线蓝牙降噪耳机WF-1000XM3就是如此
2018家用显示器多大好 6款1000左右23-27英寸显示器推
现在千元左右就能买到2K显示器了,但对于家庭用户来说,2K分辨率还是太超前了。一方面,千元左右的2K产品多是23英寸产品,点距太小用着会
这两天拜读了吴建斌的“我在碧桂园的1000天”,记录下几段自己觉得好的文字: “作为成功的企业都十分重视管理、业务及技术学习。所