设为首页 加入收藏

TOP

C语言字符串拷贝函数的实例
2013-04-10 10:08:20 来源: 作者: 【 】 浏览:406
Tags:语言 字符串 拷贝 函数 实例

  #include <string.h> /* strcpy */

  void SafeCopy(char *Dest, int DestSize, char *Source);

  main()

  {

  char Text1[20]="Tracy Sorrell";  /* string buffer */

  char Text2 ="Martin";         /* string buffer */

  printf (" Original string contents are: %s\n", Text2);

  SafeCopy(Text2, sizeof(Text2), Text1);

  printf (" New string contents are: %s\n", Text2);

  strcpy(Text2, "Alex");

  printf (" Final string contents are: %s\n", Text2);

  }

  /****************************************************************/

  void SafeCopy(

  char     *Dest,                   /* Destination buffer. */

  int       DestSize,

  char     *Source)                 /* Source data. */

  {

  /* ... Copy 'Source' into 'Dest'.

  * ...        'Dest' is padded with NULLs if 'Source' is smaller.. */

  strncpy(Dest, Source, DestSize);

  /* ... Safety net! Add the NULL just in case 'Source' is larger

  * ... than 'Dest'.  */

  Dest[DestSize-1] = '\0';

  }

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言时间函数及使用实例 下一篇C语言实现合并排序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: