03-C language advanced-simulate and implement string manipulation functions


For details of string manipulation functions, please refer to the previous blog
03-C language advanced-character functions and string functions
Github source code download address:
https://github.com/Kyrie-leon/C_Practice/tree/main/day7

1. Simulate the realization of strcat

//1.模拟实现strcat
char * my_strcat(char *destination, const char * source)
{
    
    
	//首先定义一个指针指向destination起始地址,便于操作完成后返回原始字符串的首地址
	char *dest = destination;
	//判断两个字符串是否为空
	assert(destination != NULL);
	assert(source != NULL);

	//第一轮循环让destination指向\0
	while (*destination)
	{
    
    
		destination++;
	}
	//第二轮循环将source内容追加给destination
	while ((*destination++ = *source++))
	{
    
    
		;
	}
	//返回dest起始地址
	return dest;
}

2. Simulate the realization of strcmp

//2.模拟实现strcmp
int my_strcmp(const char *str1, const char *str2)
{
    
    
	//判断指针合法性
	assert(str1 != NULL);
	assert(str2 != NULL);
	//两个字符串分别从第一个字符进行比较
	while ( (*str1++) && (*str2++) )
	{
    
    

		if (*str1  > *str2)
		{
    
    
			return 1;
		}
		else if (*str1  < *str2)
		{
    
    
			return -1;
		}
		else
		{
    
    
			continue;
		}
	}
	return 0;
}

3. Simulate the realization of strcpy


//3.模拟实现strcpy
char * my_strcpy(char * destination, const char *source)
{
    
    
	//判断指针合法性
	assert(destination!=NULL);
	assert(source!=NULL);

	//先定义一个指针指向destination便于复制后返回首地址
	char *ret = destination;
	//将第二个字符串的每个字符依次复制到destination中
	while ((*destination++ = *source++))
	{
    
    
		;
	}
	return ret;
}

4. Simulate the realization of strlen

4.1 Counter mode

//4.1计数器
int my_strlen(const char *str)
{
    
    
	//定义一个计数器
	int count = 0;
	//循环扫描字符串直到'/0'
	while (*str)
	{
    
    
		//计数器+1,字符串指针+1
		count++;
		str++;
	}

	return count;

}

4.2 Do not create a temporary variable counter (recursive)

//4.2不创建临时变量计数器,采用递归的方式
int my_strlen2(const char *str)
{
    
    

	if (*str == '\0')
	{
    
    
		return 0;
	}
	else
	{
    
    
		return 1 + my_strlen2(str + 1);
	}
}

4.3 Pointer-Pointer Way

Idea: The double pointer method is used.
First, the first pointer points to the first address of the string (that is, the first address of the incoming formal parameter string variable), the second pointer is used to traverse to the end of the string, and then the second pointer value Subtracting the first pointer value is the length of the string.
For example: assuming the string "abcd", the starting address of a is 1, the first pointer s points to a, and the second pointer points to the back of d after the end of the traversal. '\0', that is, s=1, p=5, ps=4, is the length of the string.
Note: Due to the use of double pointers, the definition of formal parameters cannot use const.
Reason: The formal parameter const char * str is a constant pointer , and then a pointer variable char *p is defined . At this time, char *p=str is wrong, because str is Constant pointer, if assigned to p is to make the type of p also become a constant pointer, this approach is wrong, the type does not match .

//4.3 双指针
//a.第一个指针指向字符串首地址,第二个指针遍历到字符串结束
//b.第二个指针减去第一个指针即为字符串长度
//注意:由于使用双指针,因此形参不能使用const
int my_strlen3(char *str)
{
    
    
	//定义指针p用于遍历字符串
	char *p = str;
	//遍历字符串str
	while (*p)
	{
    
    
		p++;
	}

	return p - str;
}

5. Simulate the realization of strstr

The optimization of strstr can refer to the following blog
KMP algorithm

//5.模拟实现strstr
const char *my_strstr(const char* str, const char* sub_str)
{
    
    
	//判断指针合法性
	assert(str != NULL);
	assert(sub_str != NULL);

	//双层循环实现字符串子串查找
	//外层循环控制str起始位置,一直向后移动直到\0
	while (*str)
	{
    
    
		//定义两个指针变量方便返回字符串起始地址
		const char *p = str;
		const char *q = sub_str;
		//内层循环用于判断str字符串是否包含sub_str子串
		while ((*p == *q)&&*p&&*q)
		{
    
    

			p++;
			q++;
		}
		//如果sub_str扫描到\0说明在str中找到子串,返回str中的子串首字符的起始地址
		if (*q == '\0')
		{
    
    
			return str;
		}
		str++;
	}


	return NULL;

}

Guess you like

Origin blog.csdn.net/qq_40076022/article/details/110182514