1.strlen find the length of string
size_t strlen ( const char * str );
(1) The string takes' \ 0 'as the end flag, and the strlen function returns the number of characters that appear before' \ 0 'in the string (excluding' \ 0 ')
(2) The string pointed to by the parameter must end with '\ 0'
(3) Note that the return value of the function is size_t. Is unsigned
Simulated implementation of strlen function:
#include<stdio.h> #include<assert.h> int my_strlen(const char*str) { assert(str != NULL); int count = 0;//Counter while (*str != '\0') { count++; str++; } return count; } int main() { char string[] = "abcdef";//The string should be placed in a character array int ret = 0; ret = my_strlen(string); printf("String length is:%d\n",ret); system("pause"); return 0; }
2.strcpy copy
char * strcpy ( char * destination, const char * source );
(1) The source string must end with '\ 0'.
(2) The '\ 0' in the original string will be copied to the target space.
(3) The destination must be large enough to hold the source string.
(4) The target space must be variable.
Simulated implementation of strcpy function:
#include<stdio.h> #include<assert.h> char *my_strcpy(char *dst, const char *src) { char *ret = dst;//Save the starting address of the target space in the temporary variable ret to facilitate the chain call of the function or system. assert(dst != NULL); assert(src != NULL); while (*dst = *src)//'=' is to ensure that when copying, it also copies \ 0 to the target space to complete the copy. { dst++; src++; } return ret; } int main() { char arr1[7] = "abcdef"; char arr2[20] = { 0 }; printf("%s\n", my_strcpy(arr2, arr1)); system("pause"); return 0; }
3.strcat splicing (splicing is also a copy)
char * strcat ( char * destination, const char * source );
(1) The source string must end with '\ 0'.
(2) The destination must be large enough to hold the contents of the source string. (enough space needs to be reserved in dst to accommodate the copied content)
(3) The target space must be modifiable.
Simulation Implementation of strat function:
#include<stdio.h> #include<assert.h> char* my_strcat(char *dst, const char *src) { assert(dst != NULL); assert(src != NULL); char* ret = dst; while (*dst)//*dst is * dst! =\ 0' { dst++; } //dst -> \0 while (*dst = *src) { dst++; src++; } return ret; } int main() { const char* src = "abcd1234"; char dst[16] = "XYZ"; my_strcat(dst, src); printf("dst: %s\n", dst); system("pause"); return 0; }
4.strcmp string size comparison
int strcmp ( const char * str1, const char * str2 );
(1) The comparison rule is from left to right. As long as the specific characters are not equal, which character is large, the size of the string has nothing to do with the length (ASCII code value comparison)
(2) STR1 > STR2, returns a number greater than 0;
(3) str1=str2, return 0;
(4) STR1 < STR2, returns a number less than 0.
Simulation Implementation of strcmp function:
#include<stdio.h> #include<assert.h> int my_strcmp(const char *s1, const char *s2) { assert(s1 != NULL); assert(s2 != NULL); int ret = 0; //Type forced rotation const unsigned char* _s1 = (const unsigned char*)s1; const unsigned char* _s2 = (const unsigned char*)s2; //! (ret == (*_s1 - *_s2)): satisfied, description*_ s1 == *_s2 while (!(ret = (*_s1 - *_s2)) && *_s1)//*_ S1 is*_ s1 !=' s1 !='\ 0'' { _s1++; _s2++; } if (ret < 0) { ret = -1; } else if (ret > 0) { ret = 1; } else { ret = 0; } return ret; } int main() { const char* _s1 = "abcd1236"; const char* _s2 = "abcd1234"; int ret = my_strcmp(_s1, _s2); printf("%d\n", ret); system("pause"); return 0; }
5. STR search function
const char * strstr ( const char * str1, const char * str2 );
char * strstr ( char * str1, const char * str2 );
Simulated implementation of STR function:
#include<stdio.h> #include<assert.h> //Target: target string, searched //sub: find the substring, which needs to be found //return NULL, not found else found it! const char* my_strstr(const char *target, const char *sub) { assert(target != NULL); assert(sub != NULL); //->target const char* start = target; const char* move = NULL; //->sub const char* sub_start = NULL; //Overall mobile while (*start) { sub_start = sub; move = start; //Local comparison while (*start && *move && *move == *sub_start) { move++; sub_start++; } // if (*sub_start == '\0') { return start; } //not found start++; } return NULL; } int main() { const char* target = "abc123abcd4567123xyz"; const char* sub_str = "xyz"; printf("%s\n", my_strstr(target, sub_str)); system("pause"); return 0; }
6.memcpy copy function
void * memcpy ( void * destination, const void * source, size_t num );
(1) The function memcpy copies num bytes of data back to the memory location of destination from the location of source.
(2) This function will not stop when it encounters' \ 0 '.
(3) If there is any overlap between source and destination, the copied result is undefined.
Simulation Implementation of memcpy function:
#include<stdio.h> #include<assert.h> void* my_memcpy(void *dst, const void *src, size_t num) { assert(dst != NULL); assert(src != NULL); if (num == 0) { //TODO return dst; } unsigned char* _dst = (unsigned char*)dst; unsigned char* _src = (unsigned char*)src; while (num) { *_dst = *_src; _dst++; _src++; num--; } return dst; } int main() { char src[32] = "abcdefg"; char dst[32] = { 0 }; my_memcpy(src, src + 1, strlen(src)); my_memcpy(src+ 1, src , strlen(src) + 1);//Memory overlap problem printf("%s\n", src); system("pause"); return 0; }
7.memmove copy function
void * memmove ( void * destination, const void * source, size_t num );
(1) The difference between memcpy and memmove is that the source memory block and target memory block processed by memmove function can overlap (both functions can be used in the new version of compiler).
memmove function simulation implementation:
#include<stdio.h> #include<assert.h> void *my_memmove(void *dst, const void *src, int num) { assert(dst != NULL); assert(src != NULL); if (num <= 0) { return NULL; } //Byte unit unsigned char* _dst = (unsigned char*)dst; unsigned char* _src = (unsigned char*)src; if (_dst > _src && _dst < _src + num) { //right->left _src = _src + num - 1; _dst = _dst + num - 1; while (num) { *_dst = *_src; _dst--; _src--; num--; } } else { while (num) { *_dst = *_src; _dst++; _src++; num--; } } return dst; } int main() { char src[32] = "abcdefg"; char dst[32] = { 0 }; my_memmove(src, src + 1, strlen(src)); printf("%s\n", src); system("pause"); return 0; }