自己实现字符串的打印代码分享(不使用strcpy和strncpy)

#include<stdio.h>
#include<string.h>
//指针的知识和字符串相关知识
char *Mystrcpy(char *des,char *src) 
{
	if(des == NULL||src ==NULL){
		return NULL;
	} 
	char *bak = des;	
	while(*src != '\0'){
			*des = *src;
				des++;
				src++;
	}
	
}

char *Mystrcpy2(char *des,char *src) 
{
	if(des == NULL||src ==NULL){
		return NULL;
	} 
	char *bak = des;	
	while(*src != '\0'){
		*des++ = *src++;
	}

}

char *Mystrncpy3(char *des,char *src,int count) 
{
	if(des == NULL||src ==NULL){
		return NULL;
	} 
	char *bak = *des;
	while(*src != '\0' &&count > 0)
	{
	*des++ = *src++;
	count--;
	}
	//copy到des。但是p不变; 
	if(count>0){
		while(count>0){
			count--;
			*des++ = '\0';//复制到\0停止,然后返回des的值 
		}
		return des;
	}
	*des = '\0';//此时des指向的值是\0,所以读不出来,结束copy 
	return bak;

}


int main()
{	
	int count;
	char *p="Welcome to come to Shantou!";
	char b[128]={'\0'};
	Mystrncpy3(b,p,8);//修改这里可以实现其他的函数调用; 
	puts(b);

	return 0;
		
} 

大家看代码并自己操练一下,应该对你的认知有帮助,谢谢支持!

上一篇:临时书写


下一篇:15.自定义线性滤波