C语言实现字符串拷贝 拷贝指定长度字符串 字符串连接

void mystrcpy(char *target, char *source)
{
	while((*target = *source) != '\0')
	{
		source++;
		target++;
	}
}

void mynstrcpy(char *target, char *source, int n)
{
	int i = 0;
	while((i < n) && (*target = *source) != '\0')
	{
		source++;
		target++;
		i++;
	}
}

调用:

int main()
{

	char * ptr = "hhahahahahahahha";
	char * dest;
	dest = (char *)malloc(80 * sizeof(char));
	
//	mystrcpy(dest, ptr);

	mynstrcpy(dest, ptr, 4);
	dest[4] = '\0';
	printf("%s\n", dest);



	return 0;
}


void myStrcat(char *dest, char *source)
{
	while(*dest != '\0')
	{
		dest++;
	}

	while( (*dest = *source) != '\0')
	{
		source++;
		dest++;
	}
}

void mynStrcat(char *dest, char *source, int n)
{
	int i = 0;
	while(*dest != '\0')
	{
		dest++;
	}

	while( i < n && (*dest = *source) != '\0')
	{
		source++;
		dest++;
	}
}


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页