当前位置:网站首页>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 !!!
边栏推荐
猜你喜欢

C语言初阶——分支语句(if,switch)

分享力扣—189.轮转数组 的三种解法

用户登录-以及创建、验证短信验证码

Source code of document type full-text retrieval knowledge base management system

Differences among bio, NiO and AIO

元素显示模式:块级,行内,行内块,嵌套规范,显示模式转换

Day4 --- Flask 蓝图与Rest-ful

Prime number screening (Ehrlich sieve method, interval sieve method, Euler sieve method)

Pytorch data type and numpy data are mutually transformed

异步数据-短信验证码
随机推荐
Mysql速成
Differences among bio, NiO and AIO
后台优惠券管理
Redis cluster
322 coin change of leetcode
The concept of cloud native application and 15 characteristics of cloud native application
初识C语言——为什么每个C程序都有一个main函数
Hi3516DV300环境搭建
自己动手做一个爬虫项目
BIO、NIO、AIO区别
程序环境和预处理(上):一个程序是怎么成功运行的?
初识C语言——常量、变量
Li Hongyi machine learning team learning punch in activity day05 --- skills of network design
封装JWT
用户-注册-登录
redis发布订阅模式
分享一道关于#define的选择题(内含#define在预编译时的替换规则,程序环境和预处理相关知识)
Prime number screening (Ehrlich sieve method, interval sieve method, Euler sieve method)
【codeforces round#800 B. Paranoid String】DP
Message reliability processing