标签:== include 自己 [] col turn 自己实现 pre pac
#include <iostream> #include <string.h> using namespace std; char* Mystrcpy(char* dst,const char* src) { if(dst == NULL || src == NULL) return NULL; int i = 0; for(i = 0;src[i] != ‘\0‘;i++) { dst[i] = src[i]; } dst[i] = ‘\0‘; return dst; } char* Mystrncpy(char *dest, const char *src, size_t n) { if(dest == NULL || src == NULL) return NULL; int i; for (i = 0; i < n && src[i] != ‘\0‘; i++) dest[i] = src[i]; for ( ; i <= n; i++) dest[i] = ‘\0‘; return dest; } int main() { char dst[10]; char dst1[4]; char src[] = "hello"; Mystrcpy(dst,src); cout << dst << endl; Mystrncpy(dst1,src,3); cout << dst1 << endl; return 0;
//结果:
/*
hello
hel
*/
标签:== include 自己 [] col turn 自己实现 pre pac
原文地址:https://www.cnblogs.com/nanqiang/p/9955160.html