当前位置:网站首页>Character function and string function (Part 1)
Character function and string function (Part 1)
2022-07-28 11:56:00 【Eight days a week】
Catalog
Ten 、strerror Error message report
One 、 Study list
- Find the string length strlen
- Unlimited length string function :strcpy strcat strcmp
- Introduction to string functions with limited length strncpy strncat strncmp
- String search strstr strtok
- Error message report strerror
Two 、strlen function
size_t strlen(const char* str);
matters needing attention :
1. String must be in ‘\0’ end , because strlen The integer returned is '\0' The number of characters that appear before
2. Notice that the function returns size_t Type of , It's an unsigned integer
3. Learn to strlen Simulation Implementation of
Simulation Implementation :
// Pointer subtraction
int strlen(const char* s)
{
assert(s);
char* p = s;
while (*p++);
return p - s-1;
}
// Recursive method
int my_strlen2(const char* s)
{
assert(s);
if (*s == '\0')
return 0;
else
return my_strlen2(s+1)+1;
}
// Counting method
int my_strlen3(const char* s)
{
assert(s);
int count = 0;
while (*s)
{
count++;
s++;
}
return count;
}3、 ... and 、strcpy
char* strcpy(char * destination, const char * source );
1. This function will souce The string of is overwritten in destination On
2. Will souce Medium \0 Copy in
3. The target space has to be large enough , Used to put down the source string
4. The target space must be visible, not const type
5. Learn to simulate
Simulation Implementation :
char* my_strcpy(char* destination, const char* source)
{
assert(destination&&source);
char* s = destination;
while (*source)
{
*destination = *source;
source++;
destination++;
}
return s;
}Four 、strcat
char * strcat ( char * destination, const char * source );1. Append a copy of the source string to the destination string . Terminate the null character in destination Is overwritten by the first character of the source , And contain a null character at the end of the new string , Connect the two to destination To form , The return value is after the connection destination The first address .
2. The source string must be in '\0' end
3. The target space has to be large enough
4. The target space can be modified
5. What happens if the string appends itself ? Will fall into a dead cycle . The following will explain :
Simulation Implementation
char* my_strcat(char* destination, const char* source)
{
assert(destination&&source);
char* p = destination;
while (*destination)
destination++;
while (*source)
{
*destination = *source;
destination++;
source++;
}
return p;
}If I do something :

5、 ... and 、strcmp
int strcmp ( const char * str1, const char * str2 );1. This function starts comparing the first character of each string . If they are equal , Just continue to compare the next character , Until the characters are different or until the termination reaches ‘\0’
2. The standard stipulates :
- The first string is larger than the second string , Return greater than 0 The number of
- The first string is equal to the second string , Then return to 0
- The first string is less than the second string , Then return less than 0 The number of
3. Learn to simulate
Simulation Implementation :
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;// equal
str1++;
str2++;
}
if (*str1 > *str2)
return 1;
else
return -1;
}6、 ... and 、strncpy
char * strncpy ( char * destination, const char * source, size_t num );1. Than strcpy There is an additional length limit , the source Before num Copy characters to destination in
2. If source The length of is less than num, Then it will be copied again source Continue copying after '\0' Know to achieve num individual
7、 ... and 、strncat
char * strncat ( char * destination, const char * source, size_t num );
Than strcat There is an additional length limit , the source Before num Characters to destination On .
Simulation Implementation :
char* my_strncpy(char* destination, const char* source, size_t num)
{
assert(destination&&source);
char* p=destination;
while (num)
{
*destination = *source;
destination++;
source++;
}
return p;
}8、 ... and 、strncmp
int strncmp ( const char * str1, const char * str2, size_t num );Compare to a different character or the end of a string or num Compare all the characters .
Nine 、 String search
strstr
char * strstr ( const char *str1, const char * str2);strstr The function looks up a string , If in str1 Appeared completely in str2 This string , Then return to str2 stay str1 The address of the first letter in . If it is not found, it returns a null pointer .
Simulation Implementation :
char* my_strstr(const char* str1, const char* str2)
{
assert(str1&&str2);
char* p = str1;
char* s = str2;
while (*p)
{
str1 = p;
str2 = s;
while (*str1 && *str2 && *str1 == str2)
{
str1++;
str2++;
}
p++;
if (*str2 == '\0')
return p;
}
return NULL;
}strtok
char * strtok ( char * str, const char * sep );matters needing attention :
- sep The parameter is a string , Defines the set of characters used as separators .
- The first parameter specifies a string , It contains 0 One or more by sep A token separated by one or more separators in a string .
- strtok Function found str Middle the next delimiter and use \0 replace . And will the \0 Save the address of so that the next time you call this function, you can directly start from the last bit here to find the separator ( notes :strtok Will change the string being manipulated , So use syrtok Function segmented strings are generally temporary copies and can be modified ).
- strtok The first argument of the function is not NULL, The function will find the first tag ,strtok Function will hold its position in the string . And return the address of this section of string .
- strtok The first argument to the function is NULL, The function will start at the same position in the string that is saved , Find next tag , And return the address of this section of string .
- When the separators of a string are found, it returns NULL.
Examples of use :

Explanation of principle :

The second call, and so on .
Ten 、strerror Error message report
char * strerror ( int errnum );Return the error message corresponding to the error code ( That is to return a string )
Example :
#include <stdio.h>
#include <string.h>
#include <errno.h>// The header file that must be included
int main ()
{
FILE * pFile;
pFile = fopen ("unexist.ent","r");
if (pFile == NULL)
printf ("Error opening file unexist.ent: %s\n",strerror(errno));
//errno: Last error number
return 0;
}
Edit & Run
The principle is that once the program runs, it will produce one errno value , Use the following collocation strerror(errno) Will return a string address , This string is the error message of the program .
errno The specific value of is as follows ( Part of the )

边栏推荐
- 【补题日记】[2022牛客暑期多校2]L-Link with Level Editor I
- [diary of supplementary questions] [2022 Niuke summer school 2] h-take the elevator
- Application of mobile face stylization Technology
- P5472 [noi2019] douzhudi (expectation, Mathematics)
- Understand how to prevent tampering and hijacking of device fingerprints
- AlexNet—论文分析及复现
- Zotero document manager and its use posture (updated from time to time)
- SkiaSharp 之 WPF 自绘 拖曳小球(案例版)
- Today's sleep quality record 74 points
- What is WordPress
猜你喜欢

Minikube initial experience environment construction

The fifth generation verification code of "cloud centered, insensitive and extremely fast" is coming heavily

Byte side: how to realize reliable transmission with UDP?

直接插入排序与希尔排序

WPF layout controls are scaled up and down with the window, which is suitable for multi-resolution full screen filling applications

Excel shortcut keys (letters + numbers) Encyclopedia

游戏流程与底层实现 逐步完成

一文看懂设备指纹如何防篡改、防劫持

Autumn recruit offer harvesters, and take the offers of major manufacturers at will

Unity 一键替换场景中的物体
随机推荐
Opencv notes sorting [Hough transform]
Alexnet - paper analysis and reproduction
What is the process of switching c read / write files from user mode to kernel mode?
[geek challenge 2019] babysql-1 | SQL injection
Direct insert sort and Hill sort
STL の 概念及其应用
Can dynamic partitions be configured when MySQL is offline synchronized to ODPs
从0开发一个自己的npm包
Will PFP be the future of digital collections?
Consumer installation and configuration
Lua 中 __index、__newindex、rawget、rawset的理解
Several reincarnation stories
R language - some metrics for unbalanced data sets
Network communication protocol classification and IP address
ES6 knowledge points supplement
Digital twin rail transit: "intelligent" monitoring to clear the pain points of urban operation
R language uses oneway The test function performs one-way ANOVA
多线程与高并发(三)—— 源码解析 AQS 原理
【补题日记】[2022杭电暑期多校2]K-DOS Card
Lua对table进行深拷贝