当前位置:网站首页>How to use STR function of C language
How to use STR function of C language
2022-07-04 02:49:00 【Yisu cloud】
c Linguistic str How to use function
This article mainly explains “c Linguistic str How to use function ”, The explanation in the text is simple and clear , Easy to learn and understand , Next, please follow Xiaobian's ideas and go deeper slowly , Study and learn together “c Linguistic str How to use function ” Well !
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);
}Thank you for reading , That's all “c Linguistic str How to use function ” Content. , After learning this article , I'm sure you're right c Linguistic str I have a deeper understanding of how to use functions , The specific use needs to be verified by practice . This is billion speed cloud , Xiaobian will push you articles with more relevant knowledge points , Welcome to your attention !
边栏推荐
- Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
- The "two-way link" of pushing messages helps app quickly realize two-way communication capability
- Chapter 3.4: starrocks data import - Flink connector and CDC second level data synchronization
- ZABBIX API pulls the values of all hosts of a monitoring item and saves them in Excel
- Network byte order
- false sharing
- How to subcontract uniapp and applet, detailed steps (illustration) # yyds dry goods inventory #
- Setting methods, usage methods and common usage scenarios of environment variables in postman
- AI 助力藝術設計抄襲檢索新突破!劉芳教授團隊論文被多媒體頂級會議ACM MM錄用
- [Yugong series] February 2022 attack and defense world advanced question misc-83 (QR easy)
猜你喜欢

MySQL advanced SQL statement (1)

AI 助力藝術設計抄襲檢索新突破!劉芳教授團隊論文被多媒體頂級會議ACM MM錄用

High level application of SQL statements in MySQL database (I)

7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
![Measurement fitting based on Halcon learning [4] measure_ arc. Hdev routine](/img/3a/cf6285ae1c01bda42874eeca9fe5b1.jpg)
Measurement fitting based on Halcon learning [4] measure_ arc. Hdev routine

The boss said: whoever wants to use double to define the amount of goods, just pack up and go

Take you to master the formatter of visual studio code

The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"

Idea if a class cannot be found, it will be red

Hospital network planning and design document based on GLBP protocol + application form + task statement + opening report + interim examination + literature review + PPT + weekly progress + network to
随机推荐
Global and Chinese market of box seals 2022-2028: Research Report on technology, participants, trends, market size and share
Bugku Zhi, you have to stop him
長文綜述:大腦中的熵、自由能、對稱性和動力學
60 year old people buy medical insurance and recommend a better product
Node solves cross domain problems
PMP daily three questions (February 14, 2022)
The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
16. System and process information
Chapter 3.4: starrocks data import - Flink connector and CDC second level data synchronization
15. System limitations and options
Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
Override and virtual of classes in C #
Remember another interview trip to Ali, which ends on three sides
Take you to master the formatter of visual studio code
查詢效率提昇10倍!3種優化方案,幫你解决MySQL深分頁問題
Unity writes a character controller. The mouse controls the screen to shake and the mouse controls the shooting
Jenkins continuous integration environment construction V (Jenkins common construction triggers)
13. Time conversion function
FRP intranet penetration
Comment la transformation numérique du crédit d'information de la Chine passe - t - elle du ciel au bout des doigts?