strncat函数

举报
悦来客栈的老板 发表于 2020/12/28 23:38:52 2020/12/28
【摘要】 /*原型:extern char *strncat(char *dest,char *src,int n);用法:#include <string.h>功能:把src所指字符串的前n个字符添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。返回指向dest的...

  
  1. /*
  2. 原型:extern char *strncat(char *dest,char *src,int n);
  3. 用法:#include <string.h>
  4. 功能:把src所指字符串的前n个字符添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
  5. 说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
  6. 返回指向dest的指针。
  7. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. char *
  11. strncat(char *dst, const char *src, size_t n)
  12. {
  13. if (n != 0)
  14. {
  15. //保存两个操作字符串的首地址
  16. char *d = dst;
  17. const char *s = src;
  18. //将指向dst的指针d移到字符串末尾,指向'\0'
  19. while (*d != 0)
  20. d++;
  21. //复制过程
  22. do {
  23. //如果到达了src字符串的末尾,则复制终止
  24. if ((*d = *s++) == 0)
  25. break;
  26. //移向下一个元素
  27. d++;
  28. } while (--n != 0); //复制后剩余要复制的元素个数
  29. *d = 0; //复制完毕后,将最后一个元素置为'\0'
  30. }
  31. return dst;
  32. }
  33. int main()
  34. {
  35. char str[50] = "Hello ";
  36. char str2[50] = "World!";
  37. strcat(str, str2);
  38. strncat(str, " Goodbye World!", 3);
  39. puts(str);
  40. }


 

 

//linux源码


  
  1. #ifndef __HAVE_ARCH_STRNCAT
  2. /**
  3. * strncat - Append a length-limited, %NUL-terminated string to another
  4. * @dest: The string to be appended to
  5. * @src: The string to append to it
  6. * @count: The maximum numbers of bytes to copy
  7. *
  8. * Note that in contrast to strncpy(), strncat() ensures the result is
  9. * terminated.
  10. */
  11. char *strncat(char *dest, const char *src, size_t count)
  12. {
  13. char *tmp = dest;
  14. if (count) {
  15. while (*dest)
  16. dest++;
  17. while ((*dest++ = *src++) != 0) {
  18. if (--count == 0) {
  19. *dest = '\0';
  20. break;
  21. }
  22. }
  23. }
  24. return tmp;
  25. }
  26. EXPORT_SYMBOL(strncat);
  27. #endif


 

 

//标准C源码


  
  1. /***
  2. *strncat.c - append n chars of string to new string
  3. *
  4. * Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines strncat() - appends n characters of string onto
  8. * end of other string
  9. *
  10. *******************************************************************************/
  11. #include <cruntime.h>
  12. #include <string.h>
  13. /***
  14. *char *strncat(front, back, count) - append count chars of back onto front
  15. *
  16. *Purpose:
  17. * Appends at most count characters of the string back onto the
  18. * end of front, and ALWAYS terminates with a null character.
  19. * If count is greater than the length of back, the length of back
  20. * is used instead. (Unlike strncpy, this routine does not pad out
  21. * to count characters).
  22. *
  23. *Entry:
  24. * char *front - string to append onto
  25. * char *back - string to append
  26. * unsigned count - count of max characters to append
  27. *
  28. *Exit:
  29. * returns a pointer to string appended onto (front).
  30. *
  31. *Uses:
  32. *
  33. *Exceptions:
  34. *
  35. *******************************************************************************/
  36. char * __cdecl strncat (
  37. char * front,
  38. const char * back,
  39. size_t count
  40. )
  41. {
  42. char *start = front;
  43. while (*front++)
  44. ;
  45. front--;
  46. while (count--)
  47. if (!(*front++ = *back++))
  48. return(start);
  49. *front = '\0';
  50. return(start);
  51. }


 

文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/11949895

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。