【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;
}
相关推荐
wn5311 分钟前
【Go - 类型断言】
服务器·开发语言·后端·golang
Hello-Mr.Wang13 分钟前
vue3中开发引导页的方法
开发语言·前端·javascript
救救孩子把16 分钟前
Java基础之IO流
java·开发语言
WG_1717 分钟前
C++多态
开发语言·c++·面试
宇卿.24 分钟前
Java键盘输入语句
java·开发语言
Amo Xiang34 分钟前
2024 Python3.10 系统入门+进阶(十五):文件及目录操作
开发语言·python
friklogff1 小时前
【C#生态园】提升C#开发效率:深入了解自然语言处理库与工具
开发语言·c#·区块链
重生之我在20年代敲代码2 小时前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
爱上语文2 小时前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
编程零零七4 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql