Quantcast
Channel: C++博客-所有随笔
Viewing all articles
Browse latest Browse all 7885

(1)字符串复制

$
0
0
 1 #include "stdafx.h"
 2 #include "iostream"
 3 #include "assert.h"
 4 
 5 using namespace std;
 6 
 7 char* mystrcpy(char* dest, const char* src); // 1 const
 8 
 9 
10 int main(int argc, char* argv[])
11 {
12     printf("Hello World!\n");
13     char buf[10] = {0};
14     mystrcpy(buf, "FUCK!");
15     cout << buf << endl;
16     return 0;
17 }
18 
19 char* mystrcpy(char* dest, const char* src)
20 {
21     assert(dest!=NULL && src!=NULL);   // 2 assert
22     char* p = dest;
23     while (*p++ = *src++);
24     *p = '\0';                                    // 3 字符串结束
25     return dest;
26 }

输出:
Hello World!
FUCK!
Press any key to continue


zhiye 2014-06-12 09:11 发表评论

Viewing all articles
Browse latest Browse all 7885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>