模拟实现strcpy,strncpy,strcat,strncat,strcmp,strncmp,memcpy,memmove

1.strcpy

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

void my_strcpy(char *dest, const char *src)   //strcpy
{
	assert(src);
	assert(dest);
	while (*dest++ = *src++)
	{
		;
	}
}

int main()  
{
	char arr1[20] = {0};
	char arr2[] = "abcdef";
	my_strcpy(arr1, arr2);
	printf("%s\n",arr1);
	system("pause");
	return 0;
}

2.strncpy

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

void my_strncpy(char *dest, const char *src, int n)  //strncpy
{
	assert(src);
	assert(dest);
	while ((n--) && (*dest++ = *src++))
	{
		;
	}
}
	

int main()
{
	char arr1[20] = {0};
	char arr2[] = "abcdef";
	int n = 0;
	printf("Please Enter copy number:");
	scanf("%d",&n);
	my_strncpy(arr1, arr2, n);
	printf("%s\n",arr1);
	system("pause");
	return 0;
}

3.strcat

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

void my_strcat(char *dest, const char *src, int len) //strcat
{
	
	char *ret = dest + len;
	assert(src);
	while (*ret++ = *src++)
	{
		;
	}
}

int main()
{
	char arr1[20] = "abc";
	char arr2[] = "def";
	int len = strlen(arr1);
	my_strcat(arr1, arr2, len);
	printf("%s\n",arr1);
	system("pause");
	return 0;
}

4.strncat

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

void my_strcat(char *dest, const char *src, int len1, int len2, int n) //strncat
{
	
	char *ret = dest + len1;
	assert(dest);
	assert(src);
	assert(len2 >= n );
	while ((n--) && (*ret++ = *src++))
	{
		;
	}
}

int main()
{
	char arr1[20] = "abc";
	char arr2[] = "def";
	int len1 = strlen(arr1);
	int len2 = strlen(arr2);
	int n = 0;
	printf("Please Enter cat number:");
	scanf("%d",&n);
	my_strcat(arr1, arr2, len1, len2, n);
	printf("%s\n",arr1);
	system("pause");
	return 0;
}

5.strcmp

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

int my_strcmp(const char *dest, const char *src) //strcmp
{
	int ret = 0;
	while (!(ret = (unsigned char)*dest-(unsigned char)*src) && *dest)
	{
		dest++;
		src++;
	}
	return ret;
}

int main()
{
	int ret = 0;
	char arr1[] = "abcde";
	char arr2[] = "abcdef";
	ret = my_strcmp(arr1, arr2);
	if (ret == 0)
	{
		printf("%s = %s", arr1, arr2);
	}
	else if (ret < 0)
	{
		printf("%s < %s", arr1, arr2);
	}
	else
	{
		printf("%s > %s", arr1, arr2);
	}
	system("pause");
	return 0;
}

6.strncmp

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
int my_strncmp(const char *dest, const char *src, int n) //strncmp
{
	int ret = 0;
	assert(dest);
	assert(src);
	assert(n);
	while( (n--) && !(ret = (unsigned char)*dest-(unsigned char)*src) && *dest )
	{
		dest++;
		src++;
	}
	return ret;
}
int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abcedef";
	int n = 0;
	int ret = 0;
	int i = 0;
	scanf("%d",&n);
	ret = my_strncmp(arr1, arr2, n);
	if(ret == 0)
	{
		for(i=0; i<n; i++)
		printf("%c",arr1[i]);
		printf("=");
		for(i=0; i<n; i++)
		printf("%c",arr2[i]);
	}
	else if(ret < 0)
	{
		for(i=0; i<n; i++)
		printf("%c",arr1[i]);
		printf("<");
		for(i=0; i<n; i++)
		printf("%c",arr2[i]);
	}
	else
	{
		for(i=0; i<n; i++)
		printf("%c",arr1[i]);
		printf(">");
		for(i=0; i<n; i++)
		printf("%c",arr2[i]);
	}
	system("pause");
	return 0;
}

7.memcpy

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

void *my_memcpy(void *dest, const void *src, int n)    //my_memcpy
{
	char *pdest = (char *)dest;
	char *psrc = (char *)src;
	assert(dest);
	assert(src);
	while ((n--) && (*pdest++ = *psrc++))
	{
		;
	}
    return dest;
}

int main()
{
	char arr1[20] = {0};
	char arr2[] = "abcdef";
	int n = 0;
	printf("Please Enter n:");
	scanf("%d", &n);
	my_memcpy(arr1, arr2, n);
	printf("%s", arr1);
	system("pause");
	return 0;
}

8.memmove

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

void *myMemMove(void *dst, const void *src, int count)  //mymemmove
{
	
	char *chardst = (char *)dst;
	char *charsrc = (char *)src;
	char *endsrc = charsrc + count - 1;
	char *enddst = chardst + count - 1;
	assert(src);
	assert(dst);
	if ((charsrc < chardst) && (chardst < charsrc + count))
	{
		while (count--)
		{
			*enddst-- = *endsrc--;
		}
	}
	else
	{
		while (count--)
		{
			*chardst++ = *charsrc++;
		}
	}
	return dst;

}

int main()
{
	char arr[64] = "abcdefg";
	int n = 0;
	printf("Please Enter n:");
	scanf("%d", &n);
    myMemMove(arr+1, arr, n);
	printf("%s", arr);
	system("pause");
	return 0;
}

 新手学习,如有错误请多多指教。










你可能感兴趣的:(模拟实现strcpy,strncpy,strcat,strncat,strcmp,strncmp,memcpy,memmove)