Simulate the implementation of C language library function strlen

The C language library function strlen finds the length of a string, everyone has used it.
Today I will teach you how to simulate and implement the function of the strlen library function in C language.

Version 1 uses the while loop directly

First of all, let's prepare and do some preparatory work. The main function is written
, and then the function of strlen is realized.

int main()
{
    
    
	char arr[] = "abc";//这里把我们自己模拟实现的
	                  //命名为 my_strlen
	int ret = my_strlen(arr);//然后用ret接收返回的字符长度
	printf("%d", ret);
}

Later, we will write the implementation of my_strlen,
because we need to calculate the length of the string as an integer , so the return value is an int . The formal parameter part passes an array address , so we choose to receive the address with a pointer, so that our function header is written.

int my_strlen(char* str)
{
    
    
	int count = 0;//这里创建一个计数器统计字符个数
	while (*str != '\0')//那么我们怎么进行计数呢?
	{
    
    //这里用while循环让他每次满足条件就+1
		count++;//用指针++每次让指针指向数组的后一个地址	
		str++;//当指到\0的时候就让他停止 	     
	}
	return count;//然后我们返回他的字符长度
}

Version 2 does not create a variable and uses recursion to calculate the character length

We have completed the basic implementation of strlen before, now let’s try the recursive method.
Two conditions for recursion
1. There are restrictions . When this condition is met, the recursion will not continue.
2. Each time the recursion gets closer and closer to the limit condition.
The first thing we think about the limit condition is that the if statement
is getting closer and closer to this condition. Is this condition similar to the above while condition? Let the pointer ++ point to /0
each time Let us see below After a while, the following code may be understood

int my_strlen(char* str)
{
    
       //限制条件和while一样当读到 \0 的时候停止递归
	if (*str != '\0')
	{
    
        //既然要递归肯定每次调用my_strlen这个函数
		return 1 + my_strlen(++str);
	}//这里每次让指针前置++,先++后调用
	else//是不是就越来越接近限制条件
	{
    
    
		return 0;//但不满足条件就返回0,停止递归
	}
}

Version 3 reference library function emulates implementation of strlen

Let's refer to the library function

insert image description here

The description of strlen in the C/C++ official website cplusplus
insert image description here

The type whose return type is     size_t
szie-t means unsigned int or unsigned long
is different in different compiler types

There is also more const to modify the pointer.
const means to modify char* p as a constant variable to
limit the value of the p pointer cannot be changed , otherwise an error will be reported
. So we will also optimize the code

#include <assert.h>//assert宏的头文件
//版本3
//参照库函数模拟strlen
size_t my_strlen(const char* str)
{
    
    
	int count = 0;
	assert(str != NULL);//这里的意思是当我们传过来的字符串
	while (*str++)//是个空指针时,及时警告提示
	{
    
    
		count++;	
	}
	return count;

}

In this way, we have realized the function of strlen. Here are the detailed explanations of the three versions. I hope everyone can gain something.

Thanks for watching! I look forward to your better opinions in the comment area to make progress and communicate together.

Guess you like

Origin blog.csdn.net/qq_57761637/article/details/130834439