当前位置:网站首页>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);
}边栏推荐
- Doris学习笔记之数据表的创建
- 7-24 reduction of the simplest fraction (rolling Division)
- Thread.sleep和TimeUnit.SECONDS.sleep的区别
- pyQt界面制作(登录+跳转页面)
- Sendmail can't send mail and it's too slow to send. Solve it
- 556. The next larger element III
- Tonybot Humanoïde Robot Infrared Remote play 0630
- Plane vector addition
- Mongodb index
- Sword finger offer 28 Symmetric binary tree
猜你喜欢

表单文本框的使用(一) 选择文本

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

puzzle(016.4)多米诺效应

Tonybot humanoid robot infrared remote control play 0630

Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块

7-15 calculation of PI

Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million

Adc128s022 ADC Verilog design and Implementation

天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库

Analysis of gene family characteristics - chromosome location analysis
随机推荐
String substitution
Time conversion ()
7-19 check denomination (solve binary linear equation)
分布式事务(Seata) 四大模式详解
Zzuli:1041 sum of sequence 2
adc128s022 ADC verilog设计实现
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
如何查询淘宝天猫的宝贝类目
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Zzuli:1043 max
Strategy, tactics (and OKR)
适用于XP的DDK
提高效率 Or 增加成本,开发人员应如何理解结对编程?
动态获取权限
洛谷P4047 [JSOI2010]部落划分 题解
To improve efficiency or increase costs, how should developers understand pair programming?
7-2 and then what time (15 minutes)
Detailed explanation of four modes of distributed transaction (Seata)
Use of constraintlayout
7-23 currency conversion (using array conversion)