char *strcat( char *strDestination, const char *strSource ); |
作用:连接字符串的函数,函数返回指针,两个参数都是指针,第一个参数所指向的内存必须能容纳两个字符串连接后的大小
strSource:源字符串
strDestination:目的字符串
int main()
{
char dest[20] = "hello ";
char *src = "https://www.it610.com/article/world";
strcat(dest, src);
printf("%s\n", dest);
system("pause");
return 0;
}
【c语言实现strcat函数】二. 函数实现
1.先将目的字符串遍历完,指针指向字符串最后面
2.将源字符串赋值给目的字符串
3.打印目的字符串
#define _CRT_SECURE_NO_WARNINGS
#include.h>
#include
#include.h>
#include.h>
char *MyStrcat(char *dest, const char *src)
//将源字符串加const,防止其内部被改变,表明其为输入参数
{
char *p = dest;
assert((dest != NULL) && (src != NULL));
//对源地址和目的地址进行断言
while (*dest != '\0')//先遍历完目的字符串,指针指向目的字符串最后面
{
dest++;
}
while ((*dest++ = *src++) != '\0')//将源字符串赋给目的字符串
{
;
}
return p;
//为实现链式操作,将目的地址返回
}
int main()
{
char dest[20] = "hello ";
char *src = "https://www.it610.com/article/world";
MyStrcat(dest, src);
printf("%s\n", dest);
system("pause");
return 0;
}
打印结果
![c语言实现strcat函数](https://img.it610.com/image/info8/44562d9fbbc34d8282189b2ea0352a01.png)
文章图片
推荐阅读
- C语言|C语言的内存操作函数
- C|结构体(初识)
- C|【C语言的灵魂】-指针(2)
- C|C语言的灵魂-指针(1)
- C|数组与字符串
- C|初识C语言(2)-常量,字符串+转义字符,ASCII码,选择语句,循环语句,函数,数组,操作符
- 蓝桥杯学习|【第十三届蓝桥杯单片机省赛冲刺-巩固练习3】
- C语言|malloc函数分配内存失败的原因及解决方法
- 算法刷题|LeetCode刷题笔记-21.合并两个有序链表