catalogue
strcpy
strcpy is a string copying function. When simulating its implementation, you should pay attention to some characteristics of this function:
1. The target string should be long enough to accommodate the original string
2. The original string must end with '\ 0'
3. Its parameters are (the starting position of the target string, the starting position of the string to be copied (up to '\ 0'), and the length of the copied string)
Now let's simulate and implement it. The code is as follows
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<assert.h> //Copy the string. The target string should be long enough to accommodate the original string. The original string must end with '\ 0' void my_strcpy(char s1[], char s2[],int sz) { assert(s1 && s2); while (*s1++ = *s2++)//Copy the string to the corresponding starting position. After copying to '\ 0', the loop just ends { ; } } int main() { char a1[20] = "abc"; char a2[] = "def"; my_strcpy(a1+2, a2, sizeof(a2)/sizeof(a2[0]));//(the starting position to be copied, the content to be copied, and the length of the copy string) printf(a1);//Print the copied results return 0; }
strlen
strlen is a function to find the length of a string. It is characterized by:
1. Stop counting when '\ 0' is encountered
2. Its return type is unsigned integer, which is not simulated here
3. Its parameter is (the starting position of the requested string, and the string will not be modified)
During simulation, you only need to traverse the count from the starting position, stop counting when '\ 0' is encountered, and return the count value. The specific code is as follows
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<assert.h> //The end of '\ 0' was encountered while seeking the length of the string void my_strlen(const char* s1)//Output the result directly in the function, so you don't need to return a value { int count = 0; assert(s1); while (*s1++)//'\ 0' end loop encountered { count++;//Count, the string is + 1 for each backward bit } printf("%d ", count);//Output results } int main() { char a1[] = "abdcsad"; my_strlen(a1); return 0; }
strcmp
The function of strcmp is to compare the string size. It should be noted that the comparison is the content of the string rather than the length. Specifically, it is to compare the size of the ASCII value. During the simulation implementation, it is necessary to start from judging the first character. If it is the same, continue to traverse backward. When encountering different characters, it is necessary to compare the size of the ASCII value.
Its parameters are (first string, second string) (string is not modified during comparison)
The specific codes are as follows:
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<assert.h> //Compare the string size. The content of the string is compared, not the length int my_strcmp(const char* dest,const char* src)//The return value is of type integer { assert(dest&&src); while (*dest == *src) { if (*dest == '\0')//When you can go to "\ 0", it means that the two strings are equal, and 0 is returned directly { return 0; } dest++;//Traverse back from the first character src++; } return *dest - *src;//Compare the size of string contents, and compare the size of ASCII values where they are not equal } int main() { char s1[] = "abcd"; char s2[] = "xyz"; int ret = my_strcmp(s1, s2); if (ret < 0)//Judge the return value to verify the result { printf("<\n"); } else if (ret == 0) { printf("=\n"); } else { printf(">\n"); } return 0; }
strstr
The strstr function is used to find a substring in a string. When searching, you should compare the main string with the first character of the substring. If it is the same, you will continue to compare the next character. If it is the same all the time, when it reaches' \ 0 'of the substring, it indicates that it contains this string and returns the first found substring, If it is different, the main string traverses backward and compares with the first character of the substring again. If it traverses to the '\ 0' position of the main function, it indicates that the main function does not contain this string.
Its parameters are (main string, sub string) (the string is not modified when searching)
When writing this function, pay attention to positioning the string. The specific code is as follows:
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<assert.h> //String lookup to find substrings in a string char* my_strstr(const char* dest, const char* src)//Returns the first string found, so the type is char* { assert(dest && src); //dest and src remember the position of the main string and substring respectively char* s1; char* s2; char* c = dest; while (*c) { s1 = c;//The starting position of the main string s2 = src;//The starting position of the substring is s2, and the substring is located. When the main string is found to have the same character as the first character of the substring, it is compared. When it is not found, it can continue to compare from the first character while (*s1 &&*s2 && *s1== *s2)//When the characters are not '\ 0' and equal, enter the loop to traverse. When the same characters are found, traverse at the same time. When the characters are different or traverse to '\ 0', end the loop { s1++; s2++; } if (*s2 == '\0')//If the substring can traverse to '\ 0', it proves that it is found in the main string, so the found position is returned { return c; } c++;//If the same character is not found, the main string continues to traverse backward } return NULL;//can't find } int main() { char a1[] = "i am good"; char a2[] = "good"; char* ret = my_strstr(a1, a2); //Find the first occurrence of a2 in a1 if (ret == NULL)//Judgment result { printf("can't find\n"); } else { printf("%s\n", ret); } return 0; }
strcat
The strcat function is used to append a string. It appends the desired additional string at the target position. The points to note in this function are:
1. When appending, consider whether the target space is enough to accommodate the appended string
2. The original string must end with '\ 0'
3. The target space must be modifiable
4. Addition can only be made at the end of the target space
When simulating the implementation, first find the end of the target string, and then append the original string from the end of the target string. The specific code is as follows:
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<assert.h> //String append. When appending, consider whether the target space is sufficient. The source character must end with "\ 0" and the target space must be modifiable char* my_strcat(char*dest,const char*src) { assert(dest && src); char* ret = dest;//Returns the starting address of the destination space while (*dest)//Found end of destination string '\ 0' { dest++; } while (*dest++ = *src++)//Append source string to '\ 0' { ; } return ret; } int main() { char arr[20] = "abc"; char arr2[] = "def"; my_strcat(arr, arr2); printf("%s ", arr);//Print result verification return 0; }
memcpy
memcpy is a memory function. Its function is to copy several bytes of the starting address of the source memory to the memory address of the target space. Because its parameter is void *, it can copy different types of data. It can copy the corresponding number of bytes.
Its parameters are (copy destination, start address of copy content, number of bytes to be copied) (copy content cannot be modified)
1. During simulation implementation, other types of data are first forcibly converted to character type, because character type is the size of one byte, so all types of data can be copied.
2. When copying, copy from the starting position of the source data to the starting position of the target, and then traverse the copy backward. Since the pointer of void * wants to be used across platforms, it is better not to use the self increasing writing method
3. After copying, return to the starting address of the target space.
4. When the copied contents overlap in memory, they will be overwritten. Only non overlapping memory copies can be completed
The specific codes are as follows:
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<assert.h> void* my_memcpy(void* dest, const void* src, size_t count) { void* ret = dest;//Returns the starting address of the destination space assert(dest && src); while (count--) { *(char*)dest=*(char*)src;//When converted to char type, it can be converted to the size of one byte and moved to the target position from the first byte dest = (char*)dest + 1;//Since it is a pointer to void *, you can't add + + directly. Use + 1 src = (char*)src + 1; } return ret; } int main() { int i = 0; int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; int arr2[20] = { 0 }; my_memcpy(arr2, arr, 10*sizeof(int));//(destination of copy, starting address of copy content, number of bytes to be copied) for (; i < 20; i++)//Print the copied array and verify the function { printf("%d ", arr2[i]); } return 0; }
memove
memove can be regarded as an upgraded version of memcpy function. It can complete the copy task when memory overlaps. The reason for the overlap is that the target location is behind the copied content, and the copied content will overwrite the later content when copying. memove can solve this problem. It only needs to judge the target location and the starting location of source data on the basis of memcpy, If the target location is in front of the source data address, the memcpy method is OK. If it is later, the data must be copied from the back to the front, and one byte from the last byte of the source data to the front, so as not to cause the problem of memory overlap.
The specific codes are as follows:
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<assert.h> //Analog memove //Complete the copy function, which can not only realize memory non overlap, but also realize memory overlap void* my_memove(void* dest, const void* src, size_t count)//Because the memory needs to be moved and the specific type of the specific copy is not determined, the parameter uses void * to return the starting address of the target space, so the return type also uses void* { void* ret = dest;//Returns the starting address of the destination space assert(src && dest); if (dest < src)//The target location is in front of the copied content, copying from front to back { while (count--)//A byte by byte position { *(char*)dest = *(char*)src;//When converted to char type, it can be converted to the size of one byte and moved to the target position from the first byte dest = (char*)dest + 1;//Every time + 1 can move one byte from front to back src = (char*)src + 1; } } else//The target location is behind the copied content, copying from back to front { while (count--) { *((char*)dest + count) = *((char*)src + count);//Move from the last byte to the last byte of the target location, and count -- can move from the back to the front byte } } return ret; } int main() { int i = 0; int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; my_memove(arr+2, arr, 16);//(destination of copy, starting address of copy content, number of bytes to be copied) for (; i < 10; i++)//Print the copied array and verify the function { printf("%d ", arr[i]); } return 0; }