当前位置:网站首页>C language learning record -- use and analysis of string function (1)
C language learning record -- use and analysis of string function (1)
2022-04-22 04:31:00 【kongqizyd146】
actually ,c The language itself does not have any string type , Strings are usually placed in constant strings or string arrays , One that cannot be modified , A modifiable .
For string functions , Some common functions , I've seen it before , The following will write some customization of these functions .
strlen
String to '\0' As an end sign ,strlen Function returns in a string '\0' The number of characters that appear before ( It doesn't contain '\0').
The string that the argument points to must be in '\0' end .
Note that the return value of the function is size_t, It's unsigned .
Simulation Implementation .
size_t strlen (const char* str)
This is the definition of the function itself , Now I want to customize the function
#include <stdio.h>
#include <assert.h>
int my_strlen(const char* str)
{
int count = 0;
assert(str != NULL);
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
if (my_strlen("abc") - my_strlen("abcdef") > 0)
{
printf("hehe\n");
}
else
{
printf("haha\n");
}
return 0;
}
Now set the return type of this function to int, that main When comparing in a function body , Small reduced , The result is a negative number , The corresponding string will be printed . And if you do it according to the standard definition ,size_t, It's actually an unsigned integer type , Then it will always be greater than 0. However, this does not mean that there is a problem with the standard definition , Because a size will certainly be larger than 0, So in order to define it as an unsigned integer . This is the method of counter , And recursive algorithms .
int my_strlen(char* str)
{
if(*str != '\0')
1+my_strlen(str + 1);
else
return 0;
}
strcpy
The source string must be in '\0' end .
In the source string '\0' Copy to target space
The target space has to be large enough , To ensure that the source string can be stored
The target space has to be variable
Simulation Implementation .
char* strcpy(char* destination, const char* source)
Two character copy , Will be able to \0 Take it to ., Simulation implementation is the key to achieve this .
char* my_strcpy(char* dest, const char* src)
{
assert(dest != NULL);
assert(src != NULL);
char* ret = dest;
// Copy src The string pointed to dest Point to space , contain \0
while (*dest++ = *src++)
{
;
}
// Return the starting address of the destination space
return ret;
}
int main()
{
char arr1[] = "abcdefghi";
char arr2[] = "bit";
int sz = sizeof(arr2) / sizeof(arr2[0]);
my_strcpy(arr1, arr2);
return 0;
}
You can't write a string pointed to by a pointer , Because this is a constant string .
strcat
The source string must be in '\0' end .
The target space has to be large enough , To accommodate the contents of the source string
The target space has to be variable
String cannot append to itself
char* strcat(char* strDestination, const char* strSource)
append a string
int main()
{
char arr1[20] = "hello";
char arr2[] = "world";
strcat(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
When appending ,arr2 From you to arr1 Of \0 Add... At the beginning , So apart from the source string , The destination string must also have \0, For example, from e Start adding , that he\llo, Will start replacing... One by one , know \0 Transfer the past , End of the process .
#include <stdio.h>
#include <assert.h>
char* my_strcat(char* dest, const char* src)
{
assert(src != NULL);
assert(dest);
char* ret = dest;
while (*dest != '\0')
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[15] = "hello";
char arr2[] = "world";
my_strcat(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
When arr1 Read \0 when , At this time, it's equivalent to strcpy This function functions , So just add .
end .
版权声明
本文为[kongqizyd146]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220428523875.html
边栏推荐
- Matplotlib draw 3dbox
- [twelfth database operation - stored procedure]
- LeetCode 63. Different paths II
- Kotlin Foundation (XIII) nested classes, inner classes and anonymous inner classes
- Use the nohup command to mount the program and execute it in the background
- DHCP protocol workflow
- Filebeat collects log data and transfers it to redis. Different es indexes are created according to log fields through logstash
- 【openEuler】Failed to download metadata for repo ‘EPOL‘: Cannot d
- Verify simple sorting using logarithm
- H7-TOOL发布固件V2.15, 脱机烧录增加瑞萨,合泰以及IS25WP全系列SPI FLASH等(2022-04-14)
猜你喜欢

2022G2电站锅炉司炉操作证考试题库及在线模拟考试

An article tells you what analysis as a service (AAAS) is
![[logical fallacies in life] right for people, wrong for things and dilemma trap](/img/13/cfc79bf522fa0b0a37086ced6f44cd.jpg)
[logical fallacies in life] right for people, wrong for things and dilemma trap

06-Datetimes

DS18B20 temperature sensor based on 51 single chip microcomputer
![[twelfth database operation - stored procedure]](/img/8f/5ca161db5ffb465dddb9d423418fad.png)
[twelfth database operation - stored procedure]

MUI-弹出菜单

Reenter leetcode (392. Judgment subsequence)

LeetCode 63. Different paths II

Sr-te policy (Cisco) -- supplement
随机推荐
NVIDIA, Wanxiang, fengyuzhu, dream chaser Fund... Talk about "new opportunities for yuan universe and industry" - 2022 yuan universe cloud Summit
An article tells you what analysis as a service (AAAS) is
10人小团队创业,如今收入过亿,不融资的简道云是怎么做到的?
What level have you learned from your pointer? Eight pointer questions let you deepen your understanding of pointer (Part 2) - O -
DS18B20 temperature sensor based on 51 single chip microcomputer
03-DataFrame & Column
L1-048 matrix a multiplied by B (15 points)
【openEuler】Failed to download metadata for repo ‘EPOL‘: Cannot d
7_ Data analysis - Evaluation
Matplotlib draw 3dbox
Why can sqlmap run the issue table but not the fields
Iptables usage
sqlilabs(25a-26)
Unity simple UI prefix tree red dot system
L1-047 sleep (10 points)
Use the nohup command to mount the program and execute it in the background
Bi set - SSM campus second-hand book sales system + thesis
一文告诉你分析即服务(AaaS)到底是什么
Filebeat collects log data and transfers it to redis. Different es indexes are created according to log fields through logstash
Summary of using knowledge points