Learn C language quickly | 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 strlen library function in C language.

1.0 Directly use while loop

First of all, let's do some preparation work. Write the main function
, and then implement the function of strlen.

1.0 Reference code

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.
Since we want to calculate the length of the string, it is an integer , so the return value is int . An array address is passed in the formal parameter part , so we choose to use a pointer to receive the address, 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;//然后我们返回他的字符长度
}

2.0 Use recursion to calculate character length without creating variables

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 recursion is getting closer and closer to this restriction
condition. The first thing we think of as a restriction condition is the if statement.
Is it getting closer and closer to this condition ? Is it similar to the while condition above? Let the pointer ++ be pointed at /0
each time . Let us see below. After a while, you may understand the following code.

2.1 Reference code

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

3.0 Reference library function simulation implementation strlen

Let’s take a look at the library functions

Insert image description here

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

The return type of     size_t
szie-t means unsigned int or unsigned long,
which varies in different compilers.

3.1The role of const

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

3.2 Reference 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 implemented the function of strlen. Here is a detailed explanation of the 3 versions. I hope everyone can gain something.

Thanks for watching! I look forward to everyone putting forward better opinions in the comment area and making progress together.

Guess you like

Origin blog.csdn.net/LT15171009269/article/details/133958163