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

边栏推荐
- 一些知识概念
- Object stream of i/o operation (serialization and deserialization)
- Detailed explanation of boost official website search engine project
- 14. User web layer services (II)
- STL concept and its application
- 一些多参数函数的具体作用
- tolua之wrap文件的原理与使用
- Skiasharp's WPF self drawn drag ball (case version)
- Final modifier attribute
- LabVIEW AI视觉工具包(非NI Vision)下载与安装教程
猜你喜欢

Unity遇坑记之 ab包卸载失败

Detailed explanation of boost official website search engine project

Software testing and quality learning notes 1 --- black box testing

Training mode and practice of digital applied talents in Colleges and Universities under the integration of industry and education

Solutions to the disappearance of Jupiter, spyder, Anaconda prompt and navigator shortcut keys

A hundred flowers bloom in data analysis engines. Why invest heavily in Clickhouse?

Good use explosion! The idea version of postman has been released, and its functions are really powerful

tolua之wrap文件的原理与使用

LabVIEW AI视觉工具包(非NI Vision)下载与安装教程

Five Ali technical experts have been offered. How many interview questions can you answer
随机推荐
LabVIEW AI visual Toolkit (non Ni vision) download and installation tutorial
How to use JWT for authentication and authorization
Several reincarnation stories
jar 包内文件的遍历以及文件的拷贝
[diary of supplementary questions] [2022 Niuke summer multi school 2] l-link with level editor I
Start from scratch blazor server (2) -- consolidate databases
Docker runs MySQL service
Database advanced learning notes cursor
Cvpr2020 best paper: unsupervised learning of symmetric deformable 3D objects
Detailed explanation of boost official website search engine project
Article summary of MinGW installation and use
Network communication protocol classification and IP address
一些知识概念
R language - some metrics for unbalanced data sets
Lua对table进行深拷贝
什么是WordPress
游戏流程与底层实现 逐步完成
【MySQL】Got an error reading communication packets
Can dynamic partitions be configured when MySQL is offline synchronized to ODPs
如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然