当前位置:网站首页>Analysis and Simulation of strlen function
Analysis and Simulation of strlen function
2022-07-24 19:31:00 【kunmu.】
️ Function analysis and simulation implementation
List of articles
Function analysis
The function prototype :
size_t strlen (const char* str)The header file : <string.h>
effect (function):Get string length
Translation translation : Get the string at \0 The number of characters before .
Parameters :const char* str
Translation translation : Receive a character pointer .
give an example :
int main() { char str1[] = "abcdef"; // Store the string in the character array char* str = "123456"; //str Is a pointer variable , The address of the first element of the string int len1 = strlen(str1); // The address of the first element of the array name , Pass it on to strlen int len2 = strlen(str); // take str Pass to strlen return 0; }
Return type :size_t( Unsigned integer )
Translation translation : Returns a greater than or equal to 0 The positive integer .
Three methods are simulated strlen
1、 Iterative method implementation strlen
#include<assert.h>
size_t my_strlen(const char* str)
{
assert(str); // Because it needs to be right str To dereference , prevent str by NULL Cause an error
int count = 0;
while(*str++) //'\0' Of ASCLL The code value is 0, Can be used as a loop end condition
{
count++;
}
return count;
}
2、 The pointer — The pointer
The pointer — The pointer : The absolute value of subtracting two pointers is the number of elements between pointers , Premise is : Pointers to the same space can be subtracted
#include<assert.h>
size_t my_strlen(const char* str)
{
assert(str);
char* start = str;
while(*str != '\0')
{
str++; // Make str Found in the \0 Previous address
}
return str - start; // Pointers to the same space are subtracted , Return the number of elements between them
}
3、 Recursive method
#include<assert.h>
size_t my_strlen(const char* str)
{
assert(str);
if(*str != '\0')
{
return 1 + my_strlen(++str); // When str The content pointed to is not '\0' When the , Recursion ,str Point to the next content
}
else
{
return 0;
}
}
matters needing attention
1、 Understanding of return parameters
give an example :
Analyze the following code :
answer :
strlen(arr1) The result must be 3,strlen(arr2) The result must be 4, How can it print out "arr1 > arr2" As a result ?
because strlen The return type of the function is size_t The unsigned integer of , Then the result of subtracting two unsigned integers must also be unsigned integers , That is, the result must be greater than or equal to 0.
So it's using strlen When the result returned by the function compares the length of two strings , Try to use Relational operator Compare .
2、strlen Pay attention to
①:strlen The function calculates \0 The number of characters before , If there is \0, The result of the calculation will end in advance
②:strlen The parameter required by the function is an address , Cannot be constant , It can't be a wild pointer .
边栏推荐
- Jedi survive and eat chicken F12 screenshot save path reference
- Unity框架之ConfigManager【Json配置文件读写】
- Day 9 (this keyword and experiment)
- He has been in charge of the British Society of engineering and technology for 13 years, and van nugget officially retired
- Mysql database, de duplication, connection
- Mysql database, subquery, union, limit
- MySQL hidden version number
- Taokeeper environment setup
- Day 4 (item 1: household income and expenditure records)
- MySQL version 5.7.9 SQL_ mode=only_ full_ group_ By question
猜你喜欢

Emergency lighting design of large stadiums and gymnasiums
![[laser principle and application -6]:q switching element and Q drive circuit board](/img/30/e199b73fb9b0ad335f26f2378cfc45.png)
[laser principle and application -6]:q switching element and Q drive circuit board

How to export map files tutorial

How to use the interface control telerik UI for WinForms development step progress bar?

Machine learning_ Data processing and model evaluation

【校招面经】8道指针面试真题,快来检测自己掌握了几道。

Why are there loopholes in the website to be repaired

asp. Net coree file upload and download example

Create a life cycle aware MVP architecture

Sequences, time series and prediction in tessorflow quizs on coursera (II)
随机推荐
Clion configuring WSL tool chain
Emergency lighting design of large stadiums and gymnasiums
纯C实现----------尼科彻斯定理
信道状态信息(CSI)共轭相乘去噪法
MySQL1
Zooinspector Download
Mysql数据库,子查询,union,limit篇
Analysis of the basic concept of digital warehouse
MySQL index principle and query optimization "suggestions collection"
Interface component devaxpress asp Net v22.1 - new office 365 dark theme
Cesium uses czml to implement dynamic routes
【无标题】
LSTM and Gru of RNN_ Attention mechanism
Unity框架之ConfigManager【Json配置文件读写】
Interceptors and filters
Compressed string
Integer
Profile environment switching
Create a life cycle aware MVP architecture
Onemanager and cloudflare workers deployment and installation - binding domain names and using cloudflare CDN acceleration
