当前位置:网站首页>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 )

边栏推荐
- MySQL (version 8.0.16) command and description
- Excel shortcut keys (letters + numbers) Encyclopedia
- Who's the big God help? Let's see how Oracle number type parsing works. Struct{scale=15, Val
- [diary of supplementary questions] [2022 Niuke summer multi school 2] D-Link with game glitch
- Network communication protocol classification and IP address
- Object to object mapping -automapper
- Anonymous implementation class object of interface
- Object stream of i/o operation (serialization and deserialization)
- Docker runs MySQL service
- R language ggplot2 visualization: ggdensity function of ggpubr package visualizes density graph and uses stat_ overlay_ normal_ Density function superimposes positive distribution curve, custom config
猜你喜欢

108. Introduction to the usage of SAP ui5 image display control Avatar

STL concept and its application

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

Will PFP be the future of digital collections?

Application of mobile face stylization Technology

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

Unity遇坑记之 ab包卸载失败

Boutique scheme | Haitai Fangyuan full stack data security management scheme sets a "security lock" for data

Deployment and use of Minio distributed object storage

Docker runs MySQL service
随机推荐
14. User web layer services (II)
游戏流程与底层实现 逐步完成
AlexNet—论文分析及复现
Zotero document manager and its use posture (updated from time to time)
Final modifier attribute
业务可视化-让你的流程图'Run'起来(4.实际业务场景测试)
一些知识概念
Object to object mapping -automapper
Byte side: how to realize reliable transmission with UDP?
Good use explosion! The idea version of postman has been released, and its functions are really powerful
OSCache cache monitoring Refresh Tool
Database advanced learning notes cursor
R language uses LM function to build regression model and regression model for transformed data (for example, it is necessary to build regression model for X and y, but they have no linear relationshi
Techniques for visualizing large time series.
Article summary of MinGW installation and use
Untiy中控制Animation的播放速度
[pyGame practice] the super interesting bubble game is coming - may you be childlike and always happy and simple~
LabVIEW AI视觉工具包(非NI Vision)下载与安装教程
Direct insert sort and Hill sort
从0开发一个自己的npm包