【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;
}
相关推荐
一支闲人13 分钟前
C语言相关简单数据结构:双向链表
c语言·数据结构·链表·基础知识·适用于新手小白
常利兵30 分钟前
Kotlin作用域函数全解:run/with/apply/let/also与this/it的魔法对决
android·开发语言·kotlin
幼稚园的山代王43 分钟前
Kotlin-基础语法练习一
android·开发语言·kotlin
重生成为编程大王1 小时前
Java ConcurrentHashMap 深度解析
java·开发语言
John.Lewis1 小时前
数据结构初阶(19)外排序·文件归并排序的实现
c语言·数据结构·排序算法
John.Lewis1 小时前
数据结构初阶(16)排序算法——归并排序
c语言·数据结构·排序算法
tanyongxi661 小时前
C++ 特殊类设计与单例模式解析
java·开发语言·数据结构·c++·算法·单例模式
遗憾皆是温柔1 小时前
24. 什么是不可变对象,好处是什么
java·开发语言·面试·学习方法
wearegogog1232 小时前
C语言中的输入输出函数:构建程序交互的基石
c语言·开发语言·交互
Fine姐2 小时前
The Network Link Layer: 无线传感器中Delay Tolerant Networks – DTNs 延迟容忍网络
开发语言·网络·php·硬件架构