当前位置:   article > 正文

C语言 ——— 学习、使用 strcat函数 并模拟实现

C语言 ——— 学习、使用 strcat函数 并模拟实现

目录

学习strcat函数​编辑

使用strcat函数​编辑

模拟实现strcat函数 


学习strcat函数

strcat函数所需要的头文件:
#include<string.h>

strcat函数的参数解析:

将 source 字符串追加到 destination 字符串。destination 中的字符串结束标志 '\0' 被 source 的第一个字符覆盖,source 字符串后面的字符依次向后追加,且 source 字符串的 '\0' 也要追加上

source 字符串的内容不会被改变,所以可加上 const 关键字修饰

strcat函数的返回值:

返回 destination 字符串的起始位置

注意:

destination 字符串的空间要足够大,能容纳下追加的 source 字符串,否则就会报错


使用strcat函数


模拟实现strcat函数 

  1. char* my_strcat(char* destination, const char* source)
  2. {
  3. // 断言
  4. assert(destination != NULL);
  5. assert(source != NULL);
  6. // 先保存目标字符串的首地址
  7. char* ret = destination;
  8. // 找到目标字符串的'\0'
  9. while (*destination)
  10. {
  11. destination++;
  12. }
  13. // 追加
  14. while (*source)
  15. {
  16. *destination++ = *source++;
  17. }
  18. // 返回目标字符串的首地址
  19. return ret;
  20. }

代码验证:

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号