String function Summary
The strlen function is a C language function of the computer. It calculates the length of string s (unsigned int type), not including '\ 0. Strlen only works as a counter. It starts scanning from a memory location (which can start with a string, a location in the middle, or even an uncertain memory area, the counter value is returned until the first string Terminator '\ 0' is reached. 1.1 array 123456789int my_strlen (char s []) {int I = 0; while (s [I]! = '\ 0') {I ++;} return I;} 1.2 pointer 1234567891011121314 // normal pointer practice int my_strlen (const char * str) {assert (str ); // asserted int count = 0; while (* str) {count ++; str ++;} return count ;}
// Get the number of elements between the two pointers by dropping the pointer (only for the same Array) int my_strlen (const char * str) {assert (str); const char * p = str; // p is the starting position while (* str) {str ++;} return str-p ;}
// In the C language library (that is, pointer reduction) size_t _ cdecl strlen (const char * str) {const char * eos = str; while (* eos ++ ); return (eos-str-1 );}
2. The strstr function implements the strstr () function to search for the first occurrence of a string in another string. This function returns the rest of the string (from the matching point ). This function is binary secure.
char* my_strstr(char* str1, char* str2){ assert(str1); assert(str2); char* ptr = str1; while (*ptr) { char *p1 = ptr; char *p2 = str2; while (*p2==*p1) { p1++; p2++; if (*p2 == '\0') return ptr; } ptr++; }}
3. The strcpy function copies the block in the bucket with null as the exit character to another bucket. Because the string is not the primary data type in the C language, it is replaced by an implementation method. In the memory, it is composed of consecutive byte blocks, strcpy can effectively copy two strings (character pointer or string pointer) that are configured to be passed back by pointer in the memory)
// Original void my_strcpy (char * str1, const char * str2) {assert (str1); // check the pointer validity assert (str2); while (* str2) {* str1 = * str2; str1 ++; str2 ++;} * str1 = '\ 0 ';}
// Optimize * 1 void my_strcpy (char * str1, const char * str2) {assert (str1); assert (str2); while (* str1 ++ = * str2 ++) // simplified code {;}}
// Optimized * 2 // requires chained access to replace void with char *, and the return value of the function is used as the parameter char * my_strcpy of another function (char * str1, const char * str2) {assert (str1); assert (str2); char * ret = str1; while (* str1 ++ = * str2 ++) {;} return ret ;}
Ps: With strncpy function implementation, copy n characters from str2 to str1
void my_strncpy(char* str1, const char* str2, int count){ assert(str1); assert(str2); while (count) { *str1 = *str2; count--; }}
4. The strcmp function implements a string comparison function. The two strings are character-by-character (compared by ASCII value) from left to right until different characters or '\ 0' are displayed.
int my_strcmp(const char*str1,const char*str2){ assert(str1); assert(str2); while (*str1 == *str2) { if (*str1 == '\0') { return 0; } str1++; str2++; } return *str1 - *str2;}
// Method 2
int my_strcmp(const char*str1, const char*str2){ assert(str1); assert(str2); while (*str1 == *str2) { if (*str1 == '\0') { return 0; } str1++; str2++; } if (*str1 - *str2 > 0) { return 1; } else { return -1; }}
5. Implement the strcat function to connect strings. The pointer returned by the function. Both parameters are pointers. The memory address pointed to by the first parameter must be able to accommodate the size after the two strings are connected.
char* my_strcat(char* str1,const char* str2){ assert(str1); assert(str2); char *ret = str1; while (*str1) { str1++; } while (*str1++ = *str2++) { ; } return ret;}