【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;
}
相关推荐
しかし11811418 分钟前
C语言队列的实现
c语言·开发语言·数据结构·数据库·经验分享·链表
zhangpeng45554794029 分钟前
用Java写一个MVCC例子
java·开发语言
point_zg43 分钟前
Vue报错...properly without JavaScript enabled. Please enable it to continue
开发语言·javascript·vue
普通网友1 小时前
如何在CentOS部署青龙面板并实现无公网IP远程访问本地面板
开发语言·后端·golang
sa100271 小时前
基于Python的网络爬虫技术研究
开发语言·爬虫·python
API小爬虫1 小时前
如何利用 Java 爬虫获取京东商品详情信息
java·开发语言·爬虫
杰杰批1 小时前
第十四届蓝桥杯大赛软件赛国赛C/C++研究生组
c语言·c++·蓝桥杯
zheshiyangyang2 小时前
JavaScript---原型和原型链
开发语言·前端·javascript
pumpkin845142 小时前
理解 Rust 中的 String 分配机制
开发语言·rust
小宁学技术2 小时前
MATLAB在工程领域的实际应用案例
开发语言·matlab·matlab实际应用案例·matlab应用案例·matlab 实际应用案例