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函数

复制代码
char* my_strcat(char* destination, const char* source)
{
	// 断言
	assert(destination != NULL);
	assert(source != NULL);

	// 先保存目标字符串的首地址
	char* ret = destination;

	// 找到目标字符串的'\0'
	while (*destination)
	{
		destination++;
	}

	// 追加
	while (*source)
	{
		*destination++ = *source++;
	}

	// 返回目标字符串的首地址
	return ret;
}

代码验证:

相关推荐
祈安_3 天前
C语言内存函数
c语言·后端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
czy87874754 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
m0_531237174 天前
C语言-数组练习进阶
c语言·开发语言·算法
Z9fish4 天前
sse哈工大C语言编程练习23
c语言·数据结构·算法
代码无bug抓狂人4 天前
C语言之单词方阵——深搜(很好的深搜例题)
c语言·开发语言·算法·深度优先
CodeJourney_J4 天前
从“Hello World“ 开始 C++
c语言·c++·学习
枫叶丹45 天前
【Qt开发】Qt界面优化(七)-> Qt样式表(QSS) 样式属性
c语言·开发语言·c++·qt
with-the-flow5 天前
从数学底层的底层原理来讲 random 的函数是怎么实现的
c语言·python·算法
Sunsets_Red5 天前
P8277 [USACO22OPEN] Up Down Subsequence P 题解
c语言·c++·算法·c#·学习方法·洛谷·信息学竞赛