当前位置:网站首页>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);
}边栏推荐
- 天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
- 修改数据库中的记录为什么报这个错
- String reverse order
- 【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
- Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)
- 适用于XP的DDK
- Luogu p5194 [usaco05dec]scales s solution
- 洛谷P4047 [JSOI2010]部落划分 题解
- Convert string to decimal integer
- Doris学习笔记之数据表的创建
猜你喜欢

Zzuli:1053 sine function

一文了解微分段应用场景与实现机制

tonybot 人形机器人 首次开机 0630
![洛谷P4047 [JSOI2010]部落划分 题解](/img/7f/3fab3e94abef3da1f5652db35361df.png)
洛谷P4047 [JSOI2010]部落划分 题解

Why is this error reported when modifying records in the database

Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in

编程语言:类型系统的本质

Analysis of gene family characteristics - chromosome location analysis

tonybot 人形机器人 查看端口并对应端口 0701

Dllexport and dllimport
随机推荐
Puzzle (016.3) is inextricably linked
分布式事务(Seata) 四大模式详解
Thread.sleep和TimeUnit.SECONDS.sleep的区别
Zzuli:1048 factorial table
7-6 mixed type data format input
Raft agreement
Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
Zzuli:1046 product of odd numbers
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
7-1 positive integer a+b (15 points)
7-10 stack of hats (25 points) (C language solution)
Zzuli: cumulative sum of 1050 factorials
Zzuli:1049 square sum and cubic sum
ConstraintLayout 的使用
7-2 and then what time (15 minutes)
7-16 find the set of integers that meet the given conditions
Understand the application scenario and implementation mechanism of differential segment
7-9 one way in, two ways out (25 points)
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
7-17 crawling worms (break exercise)