String function
1. strlen
size_t strlen (const char* str);
(1) It is used to find the length of a string
(2) Using strlen must be a string, and it must be parameter verified
(3) It is implemented in code as follows:
size_t strlen(const char* str) { //Two verification methods //Only null pointers can be verified here, and field pointers cannot be verified /*if (str == NULL) { return 0; }*/ //assert is "assertion", a macro assert(str != NULL); size_t size = 0; while (str[size] != '\0') { size++; } }
Note: two methods can be used for parameter verification here: one is if and the other is assert. Compared with the two methods, if is milder and assert is more intense The header file "#include < assert. H >" must be included when using assert;
Size in the code above_ T is generally an 8-byte unsigned integer
2. strcpy
char* strcpy(char* destination,const char* sourse);
(1) It is used to copy strings
(2) It is implemented in code as follows:
char* myStrcpy(char* dest, const char* src) { assert(dest != NULL); assert(src != NULL); int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; return dest; }
3. strcat
char* strcat (char* destination,const char* sourse);
(1) It is used for string splicing
(2) It is implemented in code as follows:
char* myStrcat(char* dest , const char* src ) { assert(dest != NULL); assert(src != NULL); //Find the end of dest first int destTail = 0; while (dest[destTail] != '\0') { destTail++; } //When the loop ends, destTail points to \ 0 //mystrcpy(dest + destTail, src); int i = 0; while (src[i] != '\0') { dest[destTail + i] = src[i]; i++; } //Finally, set the last position of dest to \ 0 dest[destTail + i] = '\0'; return dest; }
4. strcmp
int strcmp ( const char * str1, const char * str2 );
(1) It is used to compare whether strings are equal
(2) The rule of comparison is the dictionary order
(3) It is implemented in code as follows:
int myStrcmp(const char* str1, const char* str2) { assert(str1 != NULL); assert(str2 != NULL); const char* p1 = str1; const char* p2 = str2; while (*p1 != '\0' && *p2 != '\0') { if (*p1 < *p2) { return -1; } else if (*p1 > *p2) { return 1; } else {//Equality is comparing the next character p1++; p2++; } } //Two strings are not the same length if (*p1 < *p2) { return -1; } else if (*p1 > *p2) { return 1; } else { return 0; } }
Note: * * most programming languages are compared according to "= =", with the exception of C and Java,
Cannot use '= =' to compare two floating-point numbers to be equal**
5. strncpy
char* strncpy (char* destination,const char* source,size_t num);
(1) It is also a copy string, but it has more num to avoid the situation of insufficient dest space. It limits the length and can indeed prevent memory from crossing the boundary, but it is likely to only copy the general (which may become a bug)
(2) It is implemented in code as follows:
char* myStrncpy(char* dest, const char* src, size_t num) { assert(dest != NULL); assert(src != NULL); assert(num != 0); int i = 0; while (src[i] != '\0' && i < num) { dest[i] = src[i]; i++; } //There are two conditions that cause the cycle to end //1)src encountered \ 0, and then set the rest of dest to \ 0 //2)i=nmu, the function can be ended directly (this case will be included in the while below) /*if (i == num) { return dest; }*/ while (i < num) { dest[i] = '\0'; i++; } return dest; }
Note: 1 When num is smaller than src, \ 0 will be added later
2. When num is smaller than src, only num characters are copied, so \ 0 will not be copied
3. The value of num must be able to be accommodated by dest (the case of \ 0 should be considered)
6. strncat
char* strncat (char* destination,const char* sourse,size_t num);
(1) num strings before splicing
(2) It is implemented in code as follows:
char* myStrncat(char* dest, char* src, size_t num) { assert(dest != NULL); assert(src != NULL); assert(num != 0); //Find the end of dest first size_t destTail = 0; while (dest[destTail] != '\0') { destTail++; } size_t i = 0; while (src[i] != '\0' && i < num) { dest[destTail + i] = src[i]; i++; } //There are two cases when the cycle ends //1) SRC [i] = > \ 0, you need to set the end of DeST (dest[destTail+i]) to \ 0 //2)i==num, you also need to add \ 0 to the end of dest dest[destTail + i] = '\0'; return dest; }
7. strncmp
int strncmp (const char* destination, const char* sourse, size_t num);
(1) Only the first num characters are compared (the comparison rule is dictionary order)
(2) It is implemented in code as follows:
int myStrncmp(const char* str1, const char* str2, size_t num) { assert(str1 != NULL); assert(str2 != NULL); assert(num != 0); size_t i = 0; while (str1[i] != '\0' && str2[i] != '\0' && i < num) { if (str1[i] < str2[i]) { return -1; } else if (str1[i] > str2[i]) { return 1; } else { i++; } } //There are three cases when the cycle ends //1)str1[i]=='\0' //2)str2[i]=='\0' // These two situations are regarded as one. The one who ends first will be smaller //3)i==num if (i == num) { return 0; } return str1[i] - str2[i]; }
8. strstr
const char * strstr ( const char * str1, const char * str2 );
(1) It is used to determine whether a string contains another string
(2) It is implemented in code as follows:
char* mystrstr(const char* str1, const char* str2) { assert(str1 != null); assert(str2 != null); assert(*str1 != '\0'); assert(*str2 != '\0'); const char* black = str1; //The outer loop starts from where black starts to find the substring of str1 while (*black != '\0') { char* red = black; char* sub = str2; //The inner loop starts from black to determine whether the current substring is equal to str2 while (*red!='\0'&&*sub!='\0' &&*red == *sub) { red++; sub++; } //There are three possibilities for the end of the cycle //1.*red==\0 //2.*sub==\0 //3.*red!=*sub /*if (*red =='\0' || *red == *sub){ black++; continue; }*/ if(*sub == '\0'){ return black; } black++; } return null; }
9. strtok
char* strtok(char* str, const char* delimiters);
(1) It is used to segment strings
(2) Disadvantages of strtok: 1 It requires multiple calls to complete the function, which is complex to use
2. Different parameters during multiple calls
3. The original string will be called during the calling process
4. strtok uses static variables internally to record the location of the last call, which will lead to thread insecurity
10. memcpy
void* memcpy(void* destination,const void* source,size_t num);
(1) It copies data from one memory to another
(2) It is implemented in code as follows:
void* myMemcpy(void* dest, const void* src, size_t num) { assert(dest != NULL); assert(src != NULL); assert(num != 0); //Copy in bytes char* cdest = (char*)dest; const char* csrc = (const char*)src; for (size_t i = 0; i < num; i++) { cdest[i] = csrc[i]; } return dest; }
Note: void * is a special pointer that only knows the address but not the size (all types of variables can be assigned to void *). MEM series functions are also under the header file "#include < string. H >
11. memmove
void* memmove(void* destination,const void* source,size_t num);
(1) This function is used for string copying when strings overlap
(2) It is implemented in code as follows:
void* myMemmove(void* dest, const void* src, size_t num) { char* cdest = (char*)dest; const char* csrc = (const char*)src; if (csrc <= cdest && cdest <= csrc + num) { //Memory overlap, reverse copy for (size_t i = num; i > 0; i--) { cdest[i - 1] = csrc[i - 1]; } return dest; } else { //Copy without overlap for (size_t i = 0; i < num; i++) { cdest[i] = csrc[i]; } return dest; } }
12. memcmp
int memcmp (voidptr1,const void ptr2,size_t num);
(1) It is used to compare the size of the contents of two memories (it has no meaning)
(2) It is implemented in code as follows:
int myMemcmp(const void* ptr1, const void* ptr2, size_t num) { assert(ptr1 != NULL); assert(ptr2 != NULL); assert(num != 0); const char* cptr1 = (const char*)ptr1; const char* cptr2 = (const char*)ptr2; for (size_t i = 0; i < num; i++) { if (cptr1[i] < cptr2[i]) { return -1; } else if (cptr1[i] > cptr2[i]) { return 1; } else { continue; } } }
13. memset
void* memset(void* ptr , int value,size_t num);
(1) It is used to set the contents of a block of memory to a specified value
(2) It is implemented in code as follows:
void* myMemset(void* ptr, int value, size_t num) { assert(ptr != NULL); assert(num != 0); char* cptr = (char*)ptr; for (size_t i = 0; i < num; i++) { cptr[i] = (char)value; } return ptr; }