In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use the library functions commonly used in the C language". In the daily operation, I believe that many people have doubts about how to use the library functions commonly used in the C language. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about how to use the library functions commonly used in C language. Next, please follow the editor to study!
1.strlen
The string has'\ 0' as the closing flag, and the strlen function returns the number of characters that appear before'\ 0' in the string (excluding'\ 0').
Simulation implementation of function
1. Counting method int my_strlen (dest) {int count=0; while (dest) / / stop the loop {count++; dest++;} return count;} 2 when dest equals'\ 0'. The recursive method int my_strlen (char* dest) {int count=0; if (* destabilizing equation 0') {dest++; count++; return my_strlen (dest,count)} else return count;} 3. Int my_strlen (char* dest) {char* arr=dest;// defines a new pointer to dest while (* arr) / / until arr points to'\ 0' {arr++;} return (arr)-(dest); / / the subtraction of the pointer represents the number of elements between the two pointers} 2.strcpy
Char* strcpy (char* destination, const char* source)
The'\ 0' in the source string is copied to the destination space.
The target space must be large enough and variable to ensure that the source string can be stored.
Simulation implementation of function
Char* my_strcpy (char* dest, const char* str) {char* arr1 = dest; while ((* dest++ = * str++)! ='\ 0') {;} return arr1;} int main () {char arr1 [] = "*"; char arr2 [] = "hello word"; my_strcpy (arr1, arr2) Printf ("% s", arr1); return 0;} 3.strcmp
Int strcmp (const char * str1, const char * str2)
If the first string is greater than the second string, a number greater than 0 is returned.
If the first string is equal to the second string, 0 is returned
If the first string is less than the second string, a number less than 0 is returned.
Simulation implementation of function
Int my_strcmp (char* arr1,char* arr2) {while (* arr1 = = * arr2) {if (* arr1 = ='\ 0' & & * arr2 = ='\ 0') {return 0;} else {arr1++; arr2++ }} if (* arr1 > * arr2) {return 1;} else if (* arr1
< *arr2) { return -1; }}4.strcat char * strcat ( char * destination, const char * source ); 函数的模拟实现 char* my_strcat(char* dest, char* str){ char* ret = dest; while (*dest) { dest++; } while (*dest++=*str++) { ; } return ret;}int main(){ char arr1[50] = "hello"; char arr2[] = "word"; char* count=my_strcat(arr1, arr2); printf("%s", count); return 0;}5.strstr char * strstr ( const char *str1, const char * str2); 函数的模拟实现 char* my_strstr(char* dest, char* str){ char* arr1 = str; char* cur = dest; while (*cur) { str = arr1; dest = cur; if (*dest != *str) { dest++; str++; } else if (*dest == *str) { while (*dest == *str) { dest++; str++; }if (*str == '\0') { return cur; } } cur++; }return NULL;}int main(){ char arr1[] = "abbbcdef"; char arr2[] = "bbc"; char* ret=my_strstr(arr1, arr2); if (NULL == ret) { printf("找不到子串\n"); } else { printf("%s\n", ret); } return 0;}6.strtok char * strtok ( char * str, const char * sep ); strtok函数的第一个参数不为 NULL ,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。 strtok函数的第一个参数为 NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标记 int main(){ char buff[] = "www.aaabbbccc@qq.com"; char buf[20] = { 0 }; strcpy(buf, buff); char* sep=".@"; char* str = NULL; for (str = strtok(buf, sep); str != NULL; str = strtok(NULL, sep)) {//NULL是从保存好的位置开始往后找 printf("%s\n", str); } return 0;}7.字符分类函数iscntrl任何控制字符isspace空白字符:空格‘ ’,换页‘\f’,换行'\n',回车‘\r’,制表符'\t'或者垂直制表符'\v'isdigit十进制数字 0~9isxdigit十六进制数字,包括所有十进制数字,小写字母a~f,大写字母A~Fislower小写字母a~zisupper大写字母A~Zisalpha字母a~z或A~Zisalnum字母或者数字,a~z,A~Z,0~9ispunct标点符号,任何不属于数字或者字母的图形字符(可打印)isgraph 任何图形字符 isprint任何可打印字符,包括图形字符和空白字符8.memcpy&memmove void * memcpy ( void * destination , const void * source , size_t num ) 函数模拟实现 void* my_memmove(void* dest,void* str , size_t count){ void* ch = dest; if (dest >Str) {while (count--) {* ((char*) dest + count) = * ((char*) str + count) }} else {while (count--) {* (char*) dest= * (char*) str; dest= (char*) dest+1; str= (char*) str+1 } int main () {int arr1 [10] = {1, int, 2, 3, 4, 5, 6, 7, 9, 10}; my_memmove (arr1+2, arr1, 20); int sz = sizeof (arr1) / sizeof (arr1 [0]); for (int I = 0; I
< sz; i++) { printf("%d", arr1[i]); } return 0;}9.memcmp int memcmp ( const void * ptr1 , const void * ptr2 , size_t num ); 函数模拟实现 void* my_memcmp(char* arr, char* arr1, int count){ while (count--) { if (*(char*)arr >* (char*) arr1) {return 1;} else if ((* (char*) arr < * (char*) arr1)) {return-1;} arr++; arr1++;} return 0 } int main () {int arr [] = {1Jing 2Jing 3Jing 4Jing 5}; int arr1 [] = {1Jing 2Jing 3Jing 4Jing 4}; int ret=my_memcmp (arr, arr1, 20); printf ("% d", ret); return 0;} at this point, the study on "how to use the library functions commonly used in C language" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.