content
1. Simulation implementation strlen
2. Simulate the implementation of strcpy
3. Simulate the realization of strcmp
4. Simulate the implementation of strcat
5. Simulate the implementation of strstr
Friends, I am your classmate Xiao Wang
Today, I will bring you a simulation implementation of a nanny-level library function.
If the writing is helpful to everyone, please pay attention to my homepage : Your three connections are my biggest motivation
(doge) The blog of Xiao Wang who learns C language well - CSDN Blog - Domain BloggerYou can also follow Xiao Wang's gitee , bitewang - Gitee.com
1. Simulate strlen
Function introduction
- The string has '\0' as the end marker, and the strlen function returns the number of characters that appear before '\0' in the string (excluding ('\0')
- The string pointed to by the parameter must end with '/0'.
- Note that the return value of the function is size_t, which is unsigned
- Learn to simulate the strlen function
Code:
2. Simulate the implementation of strcpy
Function introduction:
- Source characters must end with '\0'
- will copy '\0' in the source string to the target space
- The destination space must be large enough to hold the source string
- The target space must be variable
Do many friends have this situation when they implement strcpy?
In fact, the reason why it can't be printed here is that the '\0' in arr2 cannot be found, so I have been looking for it, causing the compiler to crash (doge )
Code
3. Simulate the implementation of strcmp
Function introduction:
- If the first string is greater than the second string, return a number greater than 0
- If the first string is equal to the second string, return 0
- If the first string is less than the second string, return a number less than 0
- Notice! strcmp is not comparing the length of the string but! ! ! Compare the size of the characters at the corresponding positions in the string, if they are the same, then compare the next pair until they are different or encounter '\0';
Code
int my_strcmp(const char*s1,const char*s2) { assert(s1 && s2); while (*s1 == *s2) { if (*s1 == '\0') { return 0;//相等 } s1++; s2++; } if (*s1 > *s2) { return 1; } else { return -1; } } int main() { char arr1[] = "abcdef"; char arr2[] = "abcdef"; int ret = strcmp(arr1, arr2); if (ret > 0) { printf(">\n"); } else if (ret == 0) { printf("==\n"); } else { printf("<\n"); } printf("%d\n", ret); }
4. Simulate strcat
Function introduction
- Source characters must end with '\0'
- The destination space must be large enough to hold the contents of the source string
- The target space must be modifiable
- How about appending the string to itself?
When we implement strcat, the following situations often occur
The reasons are as follows: 1. The location of '\0' in 1.arr1 is unknown, so that arr2 has been looking for it and then the bit is added to arr1
2. The space of arr1 is not large enough, and out-of-bounds access to memory has been formed
Code
5. Simulate the implementation of strstr
Code:
The above is the simulation implementation of the five library functions brought by Xiao Wang to friends!
If the article is helpful to everyone, it is better to give a three-link!