首页 > 其他 > 详细

字符串复制strncpy

时间:2016-02-23 11:22:11      阅读:82      评论:0      收藏:0      [点我收藏+]
1 #include "stdafx.h"
 2 #include "iostream"
 3 #include "assert.h"
 4 
 5 using namespace std;
 6 
 7 char* mystrncpy(char* dest, const char* src, int n)
 8 {
 9     assert(dest!=NULL && src!=NULL);
10     int count = 0;
11     while (*src != \0)
12     {
13         if (count >= n)
14         {
15             break;
16         }
17         dest[count++] = *src++;
18     }
19     dest[count] = \0;
20     return dest;
21 }
22 
23 int main(int argc, char* argv[])
24 {
25     printf("Hello World!\n");
26     char buf[10] = {0};
27     mystrncpy(buf, "FUCK!", 4);
28     cout << buf << endl;
29     return 0;
30 }

输出:

Hello World!
FUCK!
Press any key to continue

字符串复制strncpy

原文:http://www.cnblogs.com/zhiyewang/p/5209057.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!