本文主要是介绍Strcat和Strcpy的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include<assert.h>
#include<stdio.h>
#include<iostream>
using namespace std;
char* my_strcat(char *strDest, const char *strSrc)
{assert(strDest != NULL && strSrc != NULL);char *ptmp_Dest = strDest;//定义两个临时变量指针指向源字符串和目标字符串const char *ptmp_Src = strSrc;while(*ptmp_Dest != '\0'){++ptmp_Dest;}while(*ptmp_Src != '\0'){*ptmp_Dest++=*ptmp_Src++;//按字符顺序赋值直到遇到\0}*ptmp_Dest = '\0';//赋值完成,字符串尾部插入\0return strDest;
}
void main()
{char Dest[20] = "hello";char *Src = " linux";cout<<"OldDest = "<<Dest<<endl;//打印链接前的目标字符串my_strcat(Dest,Src);cout<<"NewDest = "<<Dest<<endl;//打印链接后的目标字符串
}
#include<iostream>
using namespace std;
char* mystrcopy(char *dest,const char *src)
{if(dest !=NULL && src != NULL){char *ptmp_dest = dest;const char *ptmp_src = src;while(*ptmp_src != '\0'){*ptmp_dest++ = *ptmp_src++;}*ptmp_dest = '\0';return dest;}
}
void main()
{char dst[10] = "beautiful";char *src = "cool";cout<<"dest = "<<dst<<endl;mystrcopy(dst,src );cout<<"dest = "<<dst<<endl;
}
这篇关于Strcat和Strcpy的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!