C++傳智筆記(3):字串copy函數技術推演代碼

來源:互聯網
上載者:User

標籤:span   ==   代碼   printf   條件   判斷   round   color   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函數技術推演代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.