当前位置:网站首页>C language string function: strlen, strcpy, strcat
C language string function: strlen, strcpy, strcat
2022-07-27 05:36:00 【vpurple__】
This blog mainly records c String functions commonly used in language strlen、strcpy、strcat、strstr.
The remaining string functions are as follows strstr、strcmp、strtok、strerror I will write in the next article .
I sincerely hope that after writing these two blogs , Can distinguish the usage of these string functions , Don't mix it up again .
Catalog
1.strlen Find string length function
strlen Simulation Implementation of function
strcpy Simulation Implementation of
strcat Simulation Implementation
1.strlen Find string length function
// Corresponding header file
include<string.h>
size_t strlen ( const char * str );
// Return value : by size_t Unsigned integer matters needing attention
1. character string '\0' As an end sign ,strlen function The return is In the string '\0' The number of characters that appear before ( It doesn't contain '\0' ).
2. The string that the argument points to must be in '\0' end .
Be careful : If strlen Measured string No, '\0' As the end ( As follows , that strlen Measured The return value is a random value .
char arr[] = { 'a','b','c' };
3. Note the of the function The return value is size_t, It's unsigned .
Be careful : Unsigned integer plus or minus unsigned integer or unsigned integer
#include<stdio.h>
#include<string.h>
int main()
{
if (strlen("abc") - strlen("abcdef") < 0)
{
printf("<\n");
}
else
{
printf(">\n");
}
return 0;
}
// Note that the running result is >
//if (strlen("abc") - strlen("abcdef") < 0)
// Because both are unsigned 3 and 6, So subtract to -3
// here -3 It will be treated as an unsigned integer and become a very large positive number >0, Avoid this kind of writing .strlen Simulation Implementation of function
#include<stdio.h>
size_t my_strlen(char* arr);
int main()
{
char arr[] = { "acdvebv" };
size_t len = my_strlen(arr);
printf("%u\n", len);//size_t Type data is used for %u Print
return 0;
}
size_t my_strlen(char* arr)// Pay attention to the parameters
{
int count = 0;
for (count = 0; arr[count] != '\0'; count++)
{
;
}
return count;
}
Be careful :size_t Type data to be used %u Print .
2.strcpy String copy
include<string.h>
char* strcpy(char * destination, const char * source );
strcpy The basic use of
#include<stdio.h>
#include<string.h>
int main()
{
char name[20] = { "aaaaaaaaaa"};
strcpy(name, "zhang\0san");
printf("%s\n", name);
return 0;
}
//name="zhangsan";
// Note that direct assignment is not allowed ,name The array name is the address , Address is a constant value , Cannot be assigned .
//
// Note that only copy to \0 Just stop
// And the \0 Also copy to name in , So at this time, the output content of the screen is zhang
matters needing attention
1. The source string must be in '\0' end .
Be careful : When the string does not begin with '\0' when , After being copied, the code is garbled .( As shown in the figure below ).

2. In the source string '\0' Copy to target space .
3. The target space has to be large enough , To ensure that the source string can be stored .( Prevent the situation that you can't put it down and still have to put it down , Form cross-border visits .
4. The target space has to be variable , Cannot be a constant string that cannot be changed .

strcpy Simulation Implementation of
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strcpy(char* p, char* p1)
{
assert(p && p1);
int len = strlen(p);
int len1 = strlen(p1);
for (int i = 0; i =< len1; i++)
{
*(p + i) = *(p1 + i);
}
return *p;
}
int main()
{
char arr[20] = { "ancjeioerd" };
char arr1[10] = { "eifjje" };
my_strcpy(arr, arr1);
printf("%s\n", arr);
return 0;
}
3.strcat String append
include<stdio.h>
char * strcat ( char * destination, const char * source );
matters needing attention
1. The source string must be in '\0' end .
2. The target space must be large enough , It can hold the contents of the source string .
3. The target space must be modifiable .
4. The string cannot be appended to itself , Because of their own end sign '\0' Will be covered , The code will fall into an endless loop , Keep copying yourself , Cause the program to crash ( As shown below .


strcat Simulation Implementation
#include<stdio.h>
#include<string.h>
char* my_strcat(char* p, char* p1)
{
int len = strlen(p);
int len1 = strlen(p1);
for (int i = 0; i =< len1; i++)
{
*(p + i+len) = *(p1 + i);
}
return *p;
}
int main()
{
char arr[20] = { "ancjeioerd" };
char arr1[10] = { "eifjje" };
my_strcat(arr, arr1);
printf("%s\n", arr);
return 0;
}come on. !!! Tomorrow, we should also stick to blogging !!!
边栏推荐
猜你喜欢
随机推荐
原生token生成加密、解密
C语言进制转换以及原补反码位运算介绍
Notes Series docker installation PostgreSQL 14
Simplify the mybits framework of JDBC
Hi3516DV300环境搭建
后台频道组管理功能实现
268.missing number of leetcode
Day6 --- SQLAlchemy进阶
强制登录,七牛云上传图片
flask蓝图
Seckill system design
李宏毅机器学习组队学习打卡活动day02---回归
Three waiting methods of selenium and three processing methods of alert pop-up
程序环境和预处理(下):#define、#undef、命令行编译、条件编译、文件包含(超全整理,建议收藏!!!
通用视图,DRF视图回顾
上传七牛云的方法
Li Hongyi machine learning team learning punch in activity day03 --- error and gradient decline
Redis publish subscribe mode
C语言指针入门详细介绍
Prime number screening (Ehrlich sieve method, interval sieve method, Euler sieve method)









