模拟实现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;
}

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










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
strcpystrncpystrcmpstrcat 是 C 语言中的一些常用字符串操作函数。 1. strcpy:用于将一个字符串复制到另一个字符串中。函数原型为 `char* strcpy(char* dest, const char* src)`,其中 `dest` 是目标字符串,`src` 是源字符串。函数会将源字符串复制到目标字符串中,并返回目标字符串的指针。 2. strncpy:用于将一个字符串的部分内容复制到另一个字符串中。函数原型为 `char* strncpy(char* dest, const char* src, size_t n)`,其中 `dest` 是目标字符串,`src` 是源字符串,`n` 是要复制的字符数。如果源字符串的长度大于等于 `n`,则只会复制前 `n` 个字符;如果源字符串的长度小于 `n`,则会复制整个源字符串,并在目标字符串中剩余部分填充空字符。函数返回目标字符串的指针。 3. strcmp:用于比较两个字符串是否相等。函数原型为 `int strcmp(const char* str1, const char* str2)`,其中 `str1` 和 `str2` 分别是要比较的两个字符串。如果两个字符串相等,返回值为 0;如果第一个字符串小于第二个字符串,返回值为负数;如果第一个字符串大于第二个字符串,返回值为正数。 4. strcat:用于将一个字符串追加到另一个字符串的末尾。函数原型为 `char* strcat(char* dest, const char* src)`,其中 `dest` 是目标字符串,`src` 是要追加的字符串。函数会将源字符串追加到目标字符串的末尾,并返回目标字符串的指针。 这些函数都是 C 语言中常用的字符串操作函数,可以帮助我们进行字符串的复制、比较和拼接等操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值