当前位置:网站首页>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 !
边栏推荐
- I stepped on a foundation pit today
- Save Private Ryan - map building + voltage dp+deque+ shortest circuit
- Johnson–Lindenstrauss Lemma
- VRRP+BFD
- Is online futures account opening safe and reliable? Which domestic futures company is better?
- Override and virtual of classes in C #
- Mysql-15 aggregate function
- WordPress collection WordPress hang up collection plug-in
- Iclr2022 | ontoprotein: protein pre training integrated with gene ontology knowledge
- Crawler practice website image batch download
猜你喜欢
在尋求人類智能AI的過程中,Meta將賭注押向了自監督學習
Pagoda SSL can't be accessed? 443 port occupied? resolvent
中電資訊-信貸業務數字化轉型如何從星空到指尖?
The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
Problems and solutions of several concurrent scenarios of redis
Node write API
3D game modeling is in full swing. Are you still confused about the future?
Johnson–Lindenstrauss Lemma
[software implementation series] software implementation interview questions with SQL joint query diagram
随机推荐
There is no need to authorize the automatic dream weaving collection plug-in for dream weaving collection
查詢效率提昇10倍!3種優化方案,幫你解决MySQL深分頁問題
What are the main investment products of bond funds and what are they
LV1 previous life archives
LV1 Roche limit
Gee import SHP data - crop image
Sword finger offer 14- I. cut rope
Backpropagation formula derivation [Li Hongyi deep learning version]
Imperial cms7.5 imitation "D9 download station" software application download website source code
Global and Chinese market of handheld melanoma scanners 2022-2028: Research Report on technology, participants, trends, market size and share
[UE4] parse JSON string
The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
Global and Chinese market of digital impression system 2022-2028: Research Report on technology, participants, trends, market size and share
Love and self-discipline and strive to live a core life
12. Gettimeofday() and time()
中電資訊-信貸業務數字化轉型如何從星空到指尖?
60 year old people buy medical insurance and recommend a better product
Unity writes a character controller. The mouse controls the screen to shake and the mouse controls the shooting
CSCI 2134
Comment la transformation numérique du crédit d'information de la Chine passe - t - elle du ciel au bout des doigts?