首页 > 代码库 > C++传智笔记(3):字符串copy函数技术推演代码

C++传智笔记(3):字符串copy函数技术推演代码

字符串copy

 1 #include "stdio.h"
 2 #include "stdlib.h"
 3 #include "string.h"
 4 
 5 /*
 6 void *  __cdecl memcpy(void *, const void *, size_t);
 7 int     __cdecl memcmp(const void *, const void *, size_t);
 8 void *  __cdecl memset(void *, int, size_t);
 9 char *  __cdecl _strset(char *, int);
10 char *  __cdecl strcpy(char *p1, const char *p3);
11 char *  __cdecl strcat(char *, const char *);
12 int     __cdecl strcmp(const char *, const char *);
13 size_t  __cdecl strlen(const char *);
14 */
15 
16 int  copy_str2(char *from , char *to)
17 {
18     int ret = 0;
19     if (from ==NULL || to== NULL)
20     {
21         ret = -1;
22         printf("func copy_str2() err: %d, (from ==NULL || to== NULL)", ret);
23         return ret;
24     }
25     
26     for (; *from!=\0; from ++, to++ )
27     {
28         *to = *from;
29     }
30     *to = \0;
31     return ret;
32 }
33 
34 int  copy_str211(char *from , char *to)
35 {
36     int ret = 0;
37     if (from !=NULL && to!= NULL)                      //为接下来的循环建立判断条件
38     {
39         for (; *from!=\0; from ++, to++ )
40         {
41             *to = *from;
42         }
43         *to = \0;
44     }
45 
46     return ret;
47 }
48 
49 //因为后缀++的优先级,高于,*p;
50 void copy_str3(char *from , char *to)
51 {
52     while(*from != \0)
53     {
54 //         *to = *from;
55 //         to ++;
56 //         from ++;
57         *to ++ = *from++;
58     }
59     *to = \0;
60 }
61 
62 //因为后缀++的优先级,高于,*p;
63 void copy_str4(char *from , char *to)
64 {
65     while((*to ++ = *from++) != \0)
66     {
67         ;
68     }
69 }
70 
71 void main()
72 {
73     int rv = 0;
74     char from[100] = {0};
75     char to[100] = {0};
76     strcpy(from, "fromabc");
77     rv = copy_str2(from, to);
78     if (rv != 0)
79     {
80         printf("func copy_str2:%d", rv);
81         return ;
82     }
83 
84     printf("%s", to);
85     system("pause");
86 }

 

C++传智笔记(3):字符串copy函数技术推演代码