1.strstr
Function: find, find the position of another string in a string
find the return address
Cannot find returns a null pointer (NULL)
char *strstr ( const char *str1, const char *str2);
This is the strstr function, let’s simulate and implement this function
//Simulate the strstr function char* my_strstr(const char* str1, const char* str2) { //assertion assert(str1 & amp; & amp; str2); const char* s1 = NULL; const char* s2 = NULL; const char* cp = str1; //If the query string is empty return str1 if (*str2 == '\0') { return (char*)str1; } while (*cp) { s1 = cp;//String back position s2 = str2; //Prevent out-of-bounds access while (*s1 & amp; & amp; *s2 & amp; & amp; (*s1 == *s2)) { s1++; s2++; } if (*s2 == '\0') { return (char*)cp; } cp++; } return NULL; } int main() { char arr1[10] = "abbbcdefg"; char arr2[5] = "bbc"; char* ret = my_strstr(arr1, arr2); if (ret == NULL) { printf("Not found!\\ "); } else { printf("found!\\ %s", ret); } return 0; }
2. strtok
Function: Find separators, for example: [email protected], @ and . are separators
char * strtok ( char * str, const char * sep );
1. The sep parameter is a string that defines the set of characters used as separators
2. The first parameter specifies a string containing 0 or more tokens separated by one or more delimiters in the sep string.
3. The strtok function finds the next mark in str, ends it with \0, and returns a pointer to this mark. (Note: The strtok function will change the string being manipulated, so the strings split using the strtok function are generally temporary copied content and can be modified)
It means that this function will change [email protected] to 66\066.99, so we don’t use it directly, but copy it temporarily
4. The first parameter of the strtok function is not NULL, the function will find the first mark in str, and the strtok function will save its position in the string.
5. The first parameter of the strtok function is NULL, and the function will start at the saved position in the same string to find the next mark.
6. If no more tokens exist in the string, a NULL pointer is returned.
But it seems too complicated to write like this, if there are more separators, then you have to write countless functions
We might as well write a loop
int main() { char *p = "[email protected]"; const char* sep = ".@"; char arr[30]; char *str = NULL; strcpy(arr, p);//Copy the data and process the contents of the arr array for(str=strtok(arr, sep); str != NULL; str=strtok(NULL, sep)) { printf("%s\\ ", str); } }
3.strerror
Function: Return the error code and the corresponding error message.
char * strerror ( int errnum );
4.memcpy
Function: copy (not limited to strings, strcpy can only be used for strings)
void * memcpy ( void * destination, const void * source, size_t num );
size_t num : the unit is byte, the number of bytes to be copied
1. The function memcpy copies num bytes of data backwards from the location of the source to the memory location of the destination.
2. This function will not stop when encountering ‘\0’.
3. If there is any overlap between source and destination, the result of the copy is undefined.
Cannot have any overlap
Next, let’s simulate the implementation of the memcpy function
void* my_memcpy(void* dst, const void* src, size_t num) { void* ret = dst; assert(dst & amp; & amp; src); while (num--) { *(char*)dst = *(char*)src; dst = (char*)dst + 1; src = (char*)src + 1; } return ret; } int main() { int arr1[20] = { 0 }; int arr2[] = { 1,2,3,4,5,6,7,8,9,10 }; my_memcpy(arr1, arr2, 20); int i = 0; for (i = 0; i < 5; i ++ ) { printf("%d ", arr1[i]); } return 0; }
But memcpy has a flaw that it cannot have overlapping
The result is undefined if there is overlap
5. memmove
The memmove function is consistent with memcpy, but mommove can have overlapping
Let’s simulate the implementation of the memmove function
void* my_memmove(void* dst, const void* src, size_t count) { void* ret = dst; if (dst <= src || (char*)dst >= ((char*)src + count)) { while (count--) { *(char*)dst = *(char*)src; dst = (char*)dst + 1; src = (char*)src + 1; } } else { dst = (char*)dst + count - 1; src = (char*)src + count - 1; while (count--) { *(char*)dst = *(char*)src; dst = (char*)dst - 1; src = (char*)src - 1; } } return(ret); } int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; my_memmove(arr + 2, arr, 20); int i = 0; for (i = 0; i < 10; i ++ ) { printf("%d ", arr[i]); } return 0; }
6.memcmp
Function: compare the size
int memcmp ( const void * ptr1,
const void * ptr2,
size_t num );