当前位置:网站首页>[C language advanced level 10 -- character and string functions and their simulation implementation (1)]
[C language advanced level 10 -- character and string functions and their simulation implementation (1)]
2022-04-22 09:41:00 【Beginners of C language】
Character and string functions and their simulation implementation (1)
Preface
This article begins to learn the use of character functions and string library functions , And use the knowledge learned before to simulate and realize these library functions .
Library function recommendation query tool official website :
- MSDN(Microsoft Developer Network)
- www.cplusplus.com
- http://en.cppreference.com( English version )
- http://en.cppreference.com( Chinese version )
C Handling characters and strings is common in languages ,C The language itself has no string type , Strings are usually placed in Constant string Medium or A character array in . String constant For string functions that do not modify it .
The main content of this article includes :
1、 Find the string length
- strlen
2、 Unlimited length string function
- strcpy
- strcat
- strcmp
3、 Introduction to string functions with limited length
- strncpy
- strncat
- strncmp
1、 Function to find the length of string
In the previous article, I learned to use the general method 、 recursive 、 Pointer to simulate the implementation of library functions strlen To read the length of the string
- 【C Language foundation 4—— function (2)】7.4 practice 2
- 【C Language foundation 11—— The pointer (2)】4.2 The pointer - The pointer
- 【 Library function for reading string length strlen And its simulation implementation 】
1.1 Library function strlen
size_t strlen ( const char * str );
- The string has ‘\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
Use library functions :
int main()
{
const char* str1 = "abcdef";
const char* str2 = "bbb";
if (strlen(str2)-strlen(str1)>0)// Compare string length
{
printf("str2>str1\n");
}
else
{
printf("str1>str2\n");
}
return 0;
}
1.2 Simulate the implementation of library functions strlen
int my_strlen(const char* str)// Constant pointer , Limit strings , Can not be modified in the process of transmission
{
assert(str != NULL);// When str When null pointer , Application error
char* p = str;// Record the location of the first element address
while (*p != '\0')
{
p++;
}
return p - str;// Tail address - The first address , That's the length of the string
}
int main()
{
//int len = strlen("abcdef");
int len = my_strlen("abcdef");
printf("%d\n", len);
return 0;
}
The output results are shown in the figure below :

2、 Unlimited length string function
2.1 Library function strcpy
char* strcpy(char * destination, const char * source );
- 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
int main()
{
char a[10] = "x";
//char* a1 = "qwertyuop";// Constant string cannot be modified
char a2[] = "abcdef";
strcpy(a, a2);
printf("%s\n", a);
return 0;
}
2.2 Simulate the implementation of library functions strcpy
char* my_strcpy(char* dest, const char* src)
{
//assert(dest != NULL);
//assert(src != NULL);
assert(dest && src);// The same as the two above
char* ret = dest;// Record the starting position of the pointer
while (*dest = *src)
{
dest++;
src++;
}
/*while ( *src !='\0')// It's not simple enough { *dest = *src; dest++; src++ } *dest = '\0';*/
return ret;
}
int main()
{
char a[10] = "x";
//char* a1 = "qwertyuop";// Constant string cannot be modified
char a2[] = "abcdef";
printf("%s\n", my_strcpy(a, a2));// The chain
return 0;
}
The output results are shown in the figure below :

2.3 Library function strcat
char * strcat ( char * destination, const char * source );
- The source string must be in ‘\0’ end
- The target space must be large enough , It can hold the contents of the source string
- The target space must be modifiable
int main()
{
char a1[20] = "hello ";
char a2[] = "world";
strcat(a1, a2);
printf("%s\n", a1);
return 0;
}
2.4 Simulate the implementation of library functions strcat
char* my_strcat(char* dest, const char* src)
{
assert(src&&dest);
char* ret = dest;
// Find the end of string flag '\0'
while (*dest)
{
dest++;
}
// Start copying strings
while (*dest++ == *src++)
{
;
}
return ret;
}
int main()
{
char a1[20] = "hello ";
char a2[] = "world";
printf("%s\n", my_strcat(a1, a2));
return 0;
}
The output results are shown in the figure below :

2.5 Library function strcmp
int strcmp ( const char * str1, const char * str2 );
- 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
int main()
{
char a1[] = "abcd";
char a2[] = "abdc";
int ret= strcmp(a1, a2);
if (ret>0)
{
printf(">\n");
}
else if (ret==0)
{
printf("==\n");
}
else
{
printf("<\n");
}
printf("%d\n", ret);
return 0;
}
2.6 Simulate the implementation of library functions strcmp
int my_strcmp(const char* s1, const char* s2)
{
assert(s1&&s2);
while (*s1 == *s2)
{
if (*s1=='\0')// Consistent to the end , Represents the end of a string
{
return 0;
}
s1++;
s2++;
}
// Return difference
return *s1 - *s2;
}
int main()
{
char a1[] = "abcd";
char a2[] = "abdc";
int ret = my_strcmp(a1, a2);
if (ret>0)
{
printf(">\n");
}
else if (ret==0)
{
printf("==\n");
}
else
{
printf("<\n");
}
printf("%d\n", ret);
return 0;
}
The output results are shown in the figure below :

3、 String function with limited length
3.1 Library function strncpy
char * strncpy ( char * destination, const char * source, size_t num );
- Copy num Characters from the source string to the target space
- If the length of the source string is less than num, After copying the source string , Add... After the target 0, until num individual
int main()
{
char a1[] = "abcdef";
char a2[] = "qwe";
strncpy(a1, a2, 4);
printf("%s\n", a1);
return 0;
}
3.2 Simulate the implementation of library functions strncpy
char* my_strncpy(char* dest, const char* src, int cnt)
{
assert(dest&&src);
char *ret = dest;
while (cnt && (*dest++ = *src++) != '\0')
{
cnt--;
}
// If the number of characters in the source string is less than cnt, After copying the source string , Followed by the complement character '\0', Until cnt individual
if (cnt)
{
while (--cnt)
{
*dest++ = '\0';
}
}
return ret;
}
int main()
{
char a1[] = "abcdef";
char a2[] = "qwe";
strncpy(a1, a2, 4);
printf("%s\n", a1);
printf("%s\n", my_strncpy(a1, a2, 4));
return 0;
}
The output results are shown in the figure below :

3.3 Library function strncat
char * strncat ( char * destination, const char * source, size_t num );
- The first of the source num Character plus terminated null Append character to target
- If the length of the string in the source code is less than num, Then only copy until the null character is terminated ‘\0’ The content of
int main()
{
char a1[20] = "ab";
char a2[] = "qwe";
strncat(a1,a2,5);
printf("%s\n", a1);
return 0;
}
3.4 Simulate the implementation of library functions strncat
char* my_strncat(char* dest, const char* src,int cnt)
{
assert(dest&&src);
char* ret = dest;
while (*dest)// Move the pointer to the end of the string '\0' It's about
{
dest++;
}
while (cnt--)// Number of copied characters
{
*dest++ = *src++;
if (*dest)// Here, if you copy to characters '\0', It's over
{
return ret;
}
}
// When the length of the source string is greater than cnt when , Add one at the end '\0'
*dest = '\0';
return ret;
}
int main()
{
char a1[10] = "ab";
char a2[] = "qwe";
printf("%s\n", my_strncat(a1, a2, 5));
return 0;
}
The output results are shown in the figure below :

3.5 Library function strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
- The character of the first string is larger than the character of the corresponding second string , Return greater than 0 The number of
- All characters of the string are the same , Then return to 0
- The character of the first string is smaller than the character of the corresponding second string , Then return less than 0 The number of
int main()
{
char a1[] = "abcdef";
char a2[] = "abcdq";
int ret = strncmp(a1, a2, 4);
if (ret > 0)
{
printf(">\n");
}
else if (ret == 0)
{
printf("==\n");
}
else
{
printf("<\n");
}
return 0;
}
3.6 Simulate the implementation of library functions strncmp
int my_strncmp(const char* s1, const char* s2, int cnt)
{
assert(s1&&s2);
while ((cnt-1) && (*s1 == *s2))// Here for cnt It's wrong.
{
cnt--;
if (*s1 == '\0')// Consistent to the end , Represents the end of a string
{
return 0;
}
s1++;
s2++;
}
// Return difference
return *s1 - *s2;
}
//strncmp
int main()
{
char a1[] = "abcdef";
char a2[] = "abcde";
int ret = my_strncmp(a1, a2, 6);
if (ret > 0)
{
printf(">\n");
}
else if (ret == 0)
{
printf("==\n");
}
else
{
printf("<\n");
}
return 0;
}
The output results are shown in the figure below :

summary
This article has learned several library functions and their simulation code .
Continue to learn about string library functions in the next blog post .
版权声明
本文为[Beginners of C language]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220932079027.html
边栏推荐
- BI工具如何选型?这3个问题是关键
- L3-002 special stack (30 points) (two points stack)
- Cloud native enthusiast weekly: looking for open source alternatives to netlify
- Qtabelwidget instance
- QTabelWidget实例
- Zhezheng nail scanning code login
- SQL relational database management system
- LC301. 删除无效的括号
- MOS管开关频率最高多少如何测算-KIA MOS管
- 2022起重机司机(限桥式起重机)考试题库及在线模拟考试
猜你喜欢

MOS管及MOS管驱动电路案例分析-KIA MOS管

【有趣的编程题之适龄的朋友】(Leetcode原题详解)

Writing to remember the ~ goal and

一文学会text-justify,orientation,combine文本属性

Online CSV to yaml tool

Detailed explanation of p-type mos tube switch circuit and working principle - Kia MOS tube

Deep learning remote sensing scene classification data set sorting

超越iTerm! 号称下一代终端神器,功能贼强大!

How to ensure the consistency between cache and database (super detailed case)

Online yaml to properties tool
随机推荐
经典场效应管如何快速关断技巧-KIA MOS管
P8资料大放送
Heap overflow of kernel PWN basic tutorial
QT 事件过滤器实例
【C语言进阶10——字符和字符串函数及其模拟实现(1)】
SQL relational database management system
MOS管及MOS管驱动电路案例分析-KIA MOS管
Cloud native enthusiast weekly: looking for open source alternatives to netlify
virtualbox 虚拟机使用virtio-net 运行dpdk+vpp不能收取vlan报文解决
L3-005 dustbin distribution (30 points) (dijkstar)
Write a simple examination program to complete the interaction of questions and answers on the console. Questions are divided into single choice and multi choice.
好用的记笔记软件
etcd 如何实现同步监听
杰理之复位IO维持电平使用说明【篇】
Analysis of why power MOS tube is burned - Kia MOS tube
L2-030 Icelander (25 points) (nearest public ancestor)
Development of esp-01s in Arduino (1)
How to calculate the maximum switching frequency of MOS tube - Kia MOS tube
[SQL Server] SQL overview
L3-002 特殊堆栈 (30 分) (二分 栈