zoukankan      html  css  js  c++  java
  • 字符串拷贝

       1:  /*
       2:  @@
       3:      Author:    Justinzhang
       4:      Email:    uestczhangchao@gmail.com
       5:      Time:    2012-9-1 22:08:31
       6:      Desc:    It's a problem meet a long time ago. i've tried to write many times;
       7:              it also occurs at different interviews. today i write again for revies. 
       8:              God bless~
       9:  @@
      10:  */
      11:   
      12:  #include <iostream>
      13:  #include <cassert>
      14:  using namespace std;
      15:   
      16:  /*it's really a small and simple function, 
      17:  but there so many aspects to pay attention to;*/
      18:  char * strcpy(char *dest, const char *str)
      19:  {
      20:      assert(str && dest);
      21:      char *tmp = dest;
      22:      while((*dest++=*str++)!=0);//this will copy '\0' to dest
      23:      return tmp;
      24:  }
      25:   
      26:  int main()
      27:  {
      28:      char *str = "hello world!";
      29:      char *dest = new char[strlen(str)+1]; // here it add 1 to stlen(str), cause we need a '\0'
      30:      assert(dest);// whenever use a pointer, first to test if it is NULL; after use it up, assign NULL to it;
      31:      cout << strcpy(dest,str) << endl;
      32:      delete []dest;
      33:      dest = NULL;
      34:      return 0;
      35:  }
  • 相关阅读:
    C#微信公众号开发 -- (一)开发之前的准备
    C#微信公众号学习
    微信模板消息发送帮助类
    Java平台调用.net开发的WebService报错处理
    sql语句单据编号生成防并发
    [转]实用教程:搭建FTP服务器以实现局域网飞速传输文件
    C# Post Json数据到对方url
    sql语句优化技巧
    Html添加百度地图
    查看CentOS版本
  • 原文地址:https://www.cnblogs.com/justinzhang/p/2667186.html
Copyright © 2011-2022 走看看