This paper will continue to introduce the string library function, that is, the simulation implementation of the key function.
Beginning of text @ side book
8.strstr
💛 String lookup function - finds a substring in a string
Use of STR:
😇 Simulation Implementation of STR_ strlen:
💙 Train of thought analysis
1.
Process analysis:
2.
Process analysis:
Three cases of stopping matching (purple pen) are also shown here
❄️ Write code while analyzing (BF algorithm) ❄️
The small side here tries to split the steps and draw pictures for everyone, but the senses are not coherent enough. I hope all partners who see the small side here can draw their own logic after clarifying the principle. In this way, the idea will be clearer and the code will be easier to write.
Attached code:
#include<stdio.h> #include<assert.h> char* my_strstr(const char* str1, const char* str2) { assert(str1&&str2);//Assert char* cp = str1; char* s1; char* s2; while (*cp) { s1 = cp; s2 = str2; //Processing if the arr2 to be searched is an empty string if (*str2 == '\0') { return str1; } while (*s2!='\0'&&*s1!='\0'&& *s1 == *s2) { s1++; s2++; } if (*s2 == '\0') { return cp;//If found, return the first occurrence of arr2 in arr1 } cp++; } return NULL;//Can't find } int main() { char arr1[] = "i am a good student,hehe good student"; char arr2[] = "student"; char* ret = my_strstr(arr1, arr2); if (ret == NULL) { printf("can't find!\n"); } else { printf("%s\n", ret); } return 0; }
Operation results:
This is the most basic BF algorithm. What needs to be studied here is the more efficient KMP algorithm. Xiaobian will post a blog post here soon.
9.strtok
This function is weird, but you should understand it and remember it in the future.
⭐ Interpretation:
💚 Function: the strtok function divides the string str according to the specified character sequence delimiters. During segmentation, find the characters in delimiters from front to back. If found, modify the position to \ 0
💚 Note: the strtok function will change the string to be manipulated, so the string segmented by strtok is generally a temporary copy of the content and can be modified
💜 If the first parameter of strtok is not NULL, the function will find the first tag in str, and the strtok function will save its position in the string.
💜 If the first parameter of the strtok function is NULL, the function will start from the saved location and look for the next tag.
Therefore, in the first call, the first parameter must pass the address of the first element of the string to be divided
strtok(str2,"@,");
For subsequent loop calls, the first parameter must pass NULL
strtok(NULL,"@,");
💚 If there are no more tags in the string str, the NULL pointer NULL is returned
Code experience:
Here, the execution logic of the for loop is cleverly used: the initialization part is called only once.
#include<stdio.h> #include<string.h> int main() { char* p = "2965950825@qq.com";//String to be split const char* sep = ".@";//Separator char arr[30]; char* str = NULL; strcpy(arr, p);//Copy a copy of data for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep)) { printf("%s\n", str); } return 0; }
Operation results:
Operation results:
10.strerror
⭐ You can return the error information corresponding to the built-in error code in C language.
Code experience:
#include<stdio.h> #include<string.h> #Include < errno. H > / / header files that must be included int main() { FILE* pf = fopen("test.txt", "r");//file open //Errno -- if the opening fails, the error code is stored in errno if (pf == NULL) { //File opening failed, null pointer returned printf("%s\n", strerror(errno)); } else { printf("File opened successfully!\n"); } return 0; }
Operation results:
Here, strerror is only responsible for returning error information, not printing, and there is a library function perror, which is more direct:
#include<stdio.h> #Include < errno. H > / / header files that must be included int main() { FILE* pf = fopen("test.txt", "r");//file open //Errno -- if the opening fails, the error code is stored in errno if (pf == NULL) { //File opening failed, null pointer returned //printf("%s\n", strerror(errno)); perror("test:"); } else { printf("File opened successfully!\n"); } return 0; }
Operation results:
It can be seen that perror function is equivalent to print + sterror, and user-defined information can be added.
Convenient at the same time, the relative defect is that you don't want to print and you have to print.
The end @ of this topic is a general book
The best revenge is charm, and the most beautiful bloom is counterattack. Come on!