【C语言】字符函数和字符串函数的介绍和模拟实现

介绍处理字符和字符串的库函数的使用和注意事项

求字符串长度 strlen

长度不受限制的字符串函数 strcpy strcat strcmp

长度受限制的字符串函数介绍 strncpy strncat strncmp

0. 前言

C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在 常量字符串中或者字符数组 中。

字符串常量 适用于那些对它不做修改的字符串函数。

1. 函数介绍

1.1 strlen

字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。

参数指向的字符串必须要以 '\0' 结束。

注意函数的返回值为size_t,是无符号的( 易错 )

1.2 strcpy

Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point)。

源字符串必须以 '\0' 结束。

会将源字符串中的 '\0' 拷贝到目标空间。

目标空间必须足够大,以确保能存放源字符串。

目标空间必须可变。

1.3 strcat

Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

源字符串必须以 '\0' 结束。

目标空间必须有足够的大,能容纳下源字符串的内容。

目标空间必须可修改。

1.4 strcmp

This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.

标准规定:

第一个字符串大于第二个字符串,则返回大于0的数字。

第一个字符串等于第二个字符串,则返回0。

第一个字符串小于第二个字符串,则返回小于0的数字。

1.5 strncpy

Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.

拷贝num个字符从源字符串到目标空间。

如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。

1.6 strncat

Appends the first num characters of source to destination, plus a terminating null-character. If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.

将源的前num个字符追加到目标,再加上一个终止的null字符。

如果源中的C字符串的长度小于num,则只有终止之前的内容空字符被复制。

1.7 strncmp

比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。

2.模拟实现

2.1 strlen

cpp 复制代码
size_t my_strlen(const char* str)
{
	assert(str);
	const char* end = str;
	while (*++end)
	{
		;
	}
	return end - str;
}

2.2 strcpy

cpp 复制代码
char* my_strcpy(char* dest, const char* src)
{
	assert(dest && src);
	char* cp = dest;

	while (*cp++ = *src++)
	{
		;
	}
	return dest;
}

2.3 strcat

cpp 复制代码
char* my_strcat(char* dest, const char* src)
{
	assert(dest && src);
	char* cp = dest;
	while (*cp)
	{
		cp++;
	}
	while (*cp++ = *src++)
	{
		;
	}
	return dest;
}

2.4 strcmp

cpp 复制代码
int my_strcmp(const char* str1, const char* str2)
{
	assert(str1 && str2);
	while (*str1 == *str2)
	{
		if (*str1 == '\0')
		{
			return 0;
		}
		str1++;
		str2++;
	}

	return *str1 - *str2;
}

2.5 strncpy

cpp 复制代码
char* my_strncpy(char* dest, const char* src,size_t count)
{
	assert(dest && src);
	char* start = dest;
	while (count && (*dest++ = *src++) != '\0')
	{
		count--;
	}
	if (count)
	{
		*dest = '\0';
	}
	return start;
}

2.6 strncat

cpp 复制代码
char* my_strncat(char* front, const char* back, size_t count)
{
	assert(front && back);
	char* start = front;

	while (*front++)
	{
		;
	}
	front--;
	while (count--)
	{
		if ((*front++ = *back++) == '\0')
		{
			return start;
		}
	}
	*front = '\0';
	return start;
}

2.7 strncmp

cpp 复制代码
int my_strncmp(const char* str1, const char* str2, size_t count)
{
	assert(str1, str2);
	while (count-- && (*str1 == *str2))
	{
		if (count == 0 || *str1 == '\0')
		{
			return 0;
		}
		str1++;
		str2++;
	}

	return *str1 - *str2;
}
相关推荐
侃侃_天下15 小时前
最终的信号类
开发语言·c++·算法
echoarts16 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix16 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题17 小时前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说17 小时前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔17 小时前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔17 小时前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing1023292917 小时前
Day03_刷题niuke20250915
c语言
我是菜鸟0713号18 小时前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_18 小时前
QT(4)
开发语言·汇编·c++·qt·算法