【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;
}
相关推荐
坐吃山猪2 小时前
SpringBoot01-配置文件
java·开发语言
晚风(●•σ )2 小时前
C++语言程序设计——06 字符串
开发语言·c++
我叫汪枫2 小时前
《Java餐厅的待客之道:BIO, NIO, AIO三种服务模式的进化》
java·开发语言·nio
Nicole-----2 小时前
Python - Union联合类型注解
开发语言·python
晚云与城2 小时前
今日分享:C++ -- list 容器
开发语言·c++
兰雪簪轩3 小时前
分布式通信平台测试报告
开发语言·网络·c++·网络协议·测试报告
FPGAI3 小时前
Qt编程之信号与槽
开发语言·qt
Swift社区4 小时前
从 JDK 1.8 切换到 JDK 21 时遇到 NoProviderFoundException 该如何解决?
java·开发语言
0wioiw04 小时前
Go基础(④指针)
开发语言·后端·golang
房开民4 小时前
使用海康机器人相机SDK实现基本参数配置(C语言示例)
c语言·数码相机·机器人