当前位置:网站首页>C language STR function
C language STR function
2022-07-03 14:36:00 【roseisbule】
strlen:
Used to find the length of the string , Start with the first character , To '\0' end ,'\0' Not counting the total length .
Function implementation :
size_t my_strlen(const char* ptr)
{
assert(ptr);
const char* ptx = ptr;
while (*(++ptx));
return (size_t)(ptx - ptr);
}strcmp:
Used to compare two string sizes , Note that size does not refer to string length , It starts with the first character , Compare the size of characters . This function returns a int value , Different compilers , The value returned is different . But the positive and negative are consistent , When the first is greater than the second , Returns a positive value , Less than, it returns a negative value , Equal return 0.
Function implementation :
int my_strcmp(const char* str1,const char* str2)
{
assert(str1 && str2);
while((!(*str1 - *str2)) && ((*(str1++)) * (*(str2++))));
return (int)(*str1 - *str2);
}strcpy:
Used to copy strings .
Function implementation :
char* my_strcpy(char* dest,const char* source)
{
assert(dest && source);
char* result = dest;
while (*(dest++) = *(source++));
return result;
}strcat:
Used to append a string to the end of the target string .
Function implementation :
char* my_strcat(char* a, const char* b)
{
assert(a && b);
char* tmp = a;
while (*(++a));
while (*(a++) = *(b++));
*a = '\0';
return tmp;
}strstr:
Used to find another string within one string . This in KMP The algorithm is about .
Function implementation :
const char* my_strstr(const char* a, const char* b)//a Is a long string b Is a short string
{
char* cp = (char*)a;
char* s1;
char* s2;
if (!*b)
return a;
while (*cp)
{
s1 = cp;
s2 = (char*)b;
while (*s1 && *s2 && !(*s1 - *s2))
{
s1++;
s2++;
}
if (!*s2)
return cp;
cp++;
}
return NULL;
}atoi:
This function is very interesting , It will convert the numeric string of the string into int value .
Function implementation :
int my_atoi(const char* str)
{
assert(str);
int num = 0;
int result = 0;
const char* tmp = str;
while (*str && *str != '.')
{
num++;
str++;
}
while (num--)
{
result += (*tmp - '0') * (int)pow(10, num);
tmp++;
}
return result;
}strncpy,strncmp,strncat:
All three functions limit the number of characters , The function is the same .
Function implementation :
char* my_strncpy(char* dest, const char* sou, size_t num)
{
assert(dest && sou);
char* tmp = dest;
while ((num--) && (*(dest++) = *(sou++)));
return tmp;
}
char* my_strncat(char* a, const char* b, int n)
{
assert(a && b);
char* tmp = a;
while (*(++a));
while ((n--) && (*(a++) = *(b++)));
*a = '\0';
return tmp;
}
int my_strncmp(const char* str1, const char* str2,int n)
{
assert(str1 && str2);
while ((!(*str1 - *str2)) && ((*(str1++)) * (*(str2++))) && (n--));
return (int)(*str1 - *str2);
}边栏推荐
- How to query the baby category of tmall on Taobao
- adc128s022 ADC verilog设计实现
- Too many files with unapproved license
- Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
- Use of constraintlayout
- 7-22 tortoise and rabbit race (result oriented)
- Selective sorting
- 7-3 count the number of words in a line of text
- 适用于XP的DDK
- 表单文本框的使用(一) 选择文本
猜你喜欢

【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?

tonybot 人形机器人 定距移动 代码编写玩法
![Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution](/img/89/da1a3a38e02671628f385de0f30369.png)
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution

retrofit

分布式事务(Seata) 四大模式详解

tonybot 人形机器人 红外遥控玩法 0630

Puzzle (016.3) is inextricably linked

Understand the application scenario and implementation mechanism of differential segment

Niuke: crossing the river

Protobuf and grpc
随机推荐
Some concepts about agile
Zzuli: sum of 1051 square roots
天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
Output student grades
Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)
tonybot 人形机器人 红外遥控玩法 0630
洛谷P3065 [USACO12DEC]First! G 题解
String sort
Find specified characters
etcd集群权限管理和账号密码使用
tonybot 人形机器人 查看端口并对应端口 0701
1017 a divided by B (20 points)
Zzuli:1052 sum of sequence 4
Understand the application scenario and implementation mechanism of differential segment
Optical cat super account password and broadband account password acquisition
Detailed explanation of four modes of distributed transaction (Seata)
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Jiuyi cloud black free encryption free version source code
Creation of data table of Doris' learning notes
7-4 BCD decryption (10 points)