码迷,mamicode.com
首页 > 其他好文 > 详细

自己实现strcpy与strncpy

时间:2018-11-13 22:41:01      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:==   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
*/

 

自己实现strcpy与strncpy

标签:==   include   自己   []   col   turn   自己实现   pre   pac   

原文地址:https://www.cnblogs.com/nanqiang/p/9955160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!