字符函数和字符串函数解析及模拟实现


在这里插入图片描述
在这里插入图片描述


1. 求字符串长的函数

1.1strlen

size_t strlen ( const char * str );
  • 字符串以’\0’作为结束标志,strlen函数返回的是在字符串中’\0’前面出现过的字符个数(不包括’\0’)。
  • 参数指向的字符串必须以’\0’结束。
  • 注意函数的返回值位size_t, 是无符号的

1.2 strlen()模拟实现

//模拟实现的strlen()函数命名位my_strlen()函数
//模拟实现方法一
//size_t my_strlen(const char* str)
//{
    
    
//	assert(str);
//	int count = 0;
//	while (*str != '\0')
//	{
    
    
//		count++;
//		str++;
//	}
//	return count;
//}

//模拟实现方法二:指针-指针
//size_t my_strlen(const char* str)
//{
    
    
//	assert(str);
//	char* start = str;
//	while (*str)
//	{
    
    
//		str++;
//	}
//	return str - start;
//}

//模拟实现方法三:递归
size_t my_strlen(const char* str)
{
    
    
	assert(str);
	if (*str == '\0')
		return 0;
	else
		return 1 + my_strlen(str + 1);
}

2. 长度不受限制的字符串函数

2.1strcpy

 char * strcpy ( char * destination, const char * source );
  • 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’拷贝到目标空间。
  • 目标空间必须足够大,以确保能存放源字符串。

2.1.2 模拟实现

//模拟实现的strcpy()函数命名位my_strcpy()函数
char* my_strcpy(char* dest, const char* src)
{
    
    
	assert(dest && src);
	char* ret = dest;
	while (*dest++ = *src++)
	{
    
    
		;
	}
	return ret;
}

2.2 strcat

char * strcat ( char * destination, const char * source );
  • 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’结束。
  • 会将源字符串中的’\0’拷贝到目标空间。
  • 目标空间必须足够大,能容纳下源字符串中的内容。
  • 目标空间必须可修改。

2.2.1 模拟实现

char* my_strcat(char* dest, const char* src)
{
    
    
	assert(dest && src);
	char* ret = dest;
	//1.找到目标空间'\0'
	while (*dest)
	{
    
    
		dest++;
	}
	//2. 追加字符
	while (*dest++ = *src++)
	{
    
    
		;
	}
	return ret;
}

2.3 strcmp

 int strcmp ( const char * str1, const char * str2 );
  • 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的数字。

2.3.1 模拟实现

//模拟实现的strcmp()函数命名位my_strcmp()函数
int my_strcmp(const char* str1, const char* str2)
{
    
    
	assert(str1 && str2);
	while (*str1 == *str2)
	{
    
    
		str1++;
		str2++;
	}
	return *str1 - *str2;
}



3. 长度受限制的字符串函数

3.1 strncpy

 char * strncpy ( char * destination, const char * source, size_t num );
  • 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个

3.2 strncat

 char * strncat ( char * destination, const char * source, size_t num );
  • 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.

3.3 strncmp

int strncmp ( const char * str1, const char * str2, size_t num );
  • 比较到出现两个字符不同或者一个字符串结束或者num个字符全部比较完。
    在这里插入图片描述

4. 字符串查找

4.1 strstr

 const char * strstr ( const char * str1, const char * str2 );    
  • Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

4.1.2模拟实现

//模拟实现的strstr()函数命名位my_strstr()函数
char* my_strstr(const char* str1, const char* str2)
{
    
    
	assert(str1 && str2);
	char* cp = str1;
	char* s1 = cp;
	char* s2 = str2;
	
	if (*s2 == '\0')
		return NULL;
	while (*cp)
	{
    
    
		//开始匹配
		s1 = cp;
		s2 = str2;
		while(*s1 && *s2 && *s1 == *s2)
		{
    
    
			s1++;
			s2++;
		}
		if (*s2 == '\0')
			return cp;
		cp++;
	}
	return NULL;
}

4.2 strtok

char * strtok ( char * str, const char * sep );
  • sep参数是一个字符串,定义了用作分隔符的字符集合。
  • 第一个参数指定了一个字符串,它包含了0个或多个由sep字符串中一个或多个分隔符分割标记。
  • strtok函数找到str中的下一个标记,并将其用’\0’结尾,返回一个指向这个标记的指针。(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般是临时拷贝的内容并且可以修改
  • strtok函数的第一个参数不为NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。
  • strtok函数的第一个参数为NULL,函数将在同一个字符串中被保留的位置开始,查找下一个标记。
  • 如果字符串中不存在更多的标记,则返回NULL。

4.2.1 如何使用

int main()
{
    
    
	char arr1[] = "[email protected]";
	char copy[30];
	strcpy(copy, arr1);

	char arr2[] = "@.";
	char* ret = NULL;
	for (ret = strtok(copy, arr2); ret != NULL; ret = strtok(NULL, arr2))
	{
    
    
		printf("%s\n", ret);
	}
	return 0;
}

5. 错误信息报告

库函数在执行时候,发生错误会将一个错误码存放在errno这个变量中。而errno是C语言提供的一个全局变量。

5.1 strerror

char * strerror ( int errnum );
  • 返回错误码所对应的信息。

5.1.1 如何使用

#include <stdio.h>
#include <string.h>
int main()
{
    
    
	for (int i = 0; i < 10; i++)
	{
    
    
		printf("%d: %s\n", i,strerror(i));
	}
	return 0;
}

运行结果:
在这里插入图片描述
字符分类函数:
在这里插入图片描述
字符转换:

int tolower (int c)
int toupper (int c)


6. 内存操作函数

6.1 memcpy

void * memcpy ( void * destination, const void * source, size_t num );
  • 函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
  • 这个函数在遇到’\0’的时候并不会停下来
  • 如果source和destination有任何的重叠,复制的结果是未定义的

6.1.1 模拟实现

void* my_memcpy(void* dest, const void* src, size_t num)
{
    
    
	assert(dest && src);
	void* ret = dest;
	while (num--)
	{
    
    
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}

6.2 memmove

void * memmove ( void * destination, const void * source, size_t num );
  • 和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可重叠的。
  • 如果源空间和目标空间出现重叠,就得使用memmove函数处理。

6.2.1 模拟实现

void* my_memmove(void* dest, const void* src, size_t num)
{
    
    
	assert(dest && src);
	void* ret = dest;
	if (dest > src)//从后向前拷贝
	{
    
    
		while (num--)
		{
    
    
			*((char*)dest + num) = *((char*)src + num);
		}
	}
	else//从前向后拷贝
	{
    
    
		while (num--)
		{
    
    
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	return ret;
}

6.3 memcmp

int memcmp ( const void * ptr1, const void * ptr2, size_t num );
  • 比较从prt1和prt2指针开始的num个字节
  • 返回值如下:
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Zhenyu_Coder/article/details/131880483
今日推荐