字符串的追加

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

//使用系统提供的函数strcat、strncat

int main0101()

{

  char dest[100]="hello";

  char src[]="world";

//字符串追加

  //strcat(dest,src);

 

//字符串有限追加

  strncat(dest,src,3);//hellowor

  printf("%s\n",dest);

  return EXIT_SUCCESS;

}

 

//自定义函数

//字符串追加

void my_strcat01(char*dest,char*src)

{

//找到dest字符串中\0的位置

  while(*dest)dest++;

  while(*dest++=*src++);

}

//字符串有限追加

void my_strcat(char*dest,char*src,size_t n)

{

  while(*dest)dest++;

  while((*dest++=*src++&&--n);

}

int main(void)

{

  char dest[100]="hello";

  char src[]="world";

//字符串追加

  //my_strcat(dest,src);

 

//字符串有限追加

  my_strncat(dest,src,3)//hellowor

}

posted @ 2020-09-03 20:07  wh19991213  阅读(456)  评论(0编辑  收藏  举报