In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to achieve strcpy and strcat functions in C language". In daily operation, I believe many people have doubts about how to achieve strcpy and strcat functions in C language. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how C language realizes strcpy and strcat functions". Next, please follow the editor to study!
First, the introduction of strcpy function 1. Declaration of function
Char* strcpy (char* destination, const char* source)
two。 Function function and matters needing attention
Copies the string pointed by the source to the array pointed to by the destination, including the terminating null character (and stops at that point).
The source string must end with'\ 0'.
The'\ 0' in the source string is copied to the destination space.
The target space must be large enough to ensure that the source string can be stored.
The target space must be variable.
3. When using the strcpy function # include#include// remember to reference its header file int main () {char ch2 [7] = "abcdef"; char ch3 [10]; / / ch3 is the target array and the space must be larger than ch2 strcpy (ch3, ch2); printf ("% s\ n", ch3); return 0;}
Running result
Second, simulate and realize the strcpy function 1. Simulation analysis
1. We need to copy the string pointed by the source to the array pointed to by the destination, including the terminating null character, so we can copy it character by character, and note that the terminating empty character'\ 0' should also be copied in the past. If you don't copy the'\ 0', there will be a problem later if you want to print the string that assigned the value to the past.
two。 Because the string pointed to by our source does not need to be changed, we need to restrict the string pointed to by our source with const to prevent it from being changed to make it more secure.
3. To avoid passing an address with a null pointer, we need to use assert to assert that the incoming address is not a null pointer.
4. To achieve chained access, we need to return the incoming destination starting address (destination). Since this function changes the contents of the destination store when executed, we need to recreate a pointer of type char* to store this address.
two。 Simulation realization
The following code is a little dry, need fine products, it is recommended to collect so as not to forget.
# include#includechar* my_strcpy (char* dest, const char* src) {assert (dest & & src); char* ret = dest; while (* dest++ = * src++) {;} return ret;} int main () {char ch2 [7] = "abcdef"; char ch3 [10] / / ch3 is the target array, and the space must be larger than ch2 my_strcpy (ch3, ch2); / / copy the string in ch2 to ch3 printf ("% s\ n", my_strcpy (ch3, ch2)); / / chained access return 0;}
Running result
Third, the introduction of strcat function. Declaration of function
Char * strcat (char * destination, const char * source)
two。 Function function and matters needing attention
Appends the source string to the destination string. The terminating null character'\ 0' of the target string is overwritten by the first character of the source string and contains a terminating empty character'\ 0' at the end of the new string, formed by concatenating the two at the destination.
The source string must end with'\ 0'.
The target space must be large enough to hold the contents of the source string.
The target space must be modifiable.
3. Use of function # include#include// remember to reference its header file int main () {char ch2 [4] = "def"; char ch3 [7] = "abc" when using the strcat function; / / ch3 is the target array and the space must be larger than ch2 strcat (ch3, ch2); / / append the string in ch2 to ch3 printf ("% s", ch3); return 0;}
Running result
Fourth, simulate and realize the strcat function 1. Simulation analysis
1. When we append a string, we append the source string to the target string, and we need to append it from the position of'\ 0' in the target string, so we first need to find the location of'\ 0' in the target string. And after we append it, we need to append an extra'\ 0' after the string to make the new string complete.
two。 Because the string pointed to by our source does not need to be changed, we need to restrict the string pointed to by our source with const to prevent it from being changed to make it more secure.
3. To avoid passing an address with a null pointer, we need to use assert to assert that the incoming address is not a null pointer.
4. To achieve chained access, we need to return the incoming destination starting address (destination). Since this function changes the contents of the destination store when executed, we need to recreate a pointer of type char* to store this address.
two。 Simulate the implementation of # include#include char* my_strcat (char* dest, const char* src) {assert (dest & & src); / / assert that the incoming address is not a null pointer char* ret = dest;// save the destination starting address while (* dest) / / find the address {dest++ of'\ 0' } while (* dest++ = * src++) / / append string {;} return ret;} int main () {char ch2 [4] = "def"; char ch3 [7] = "abc"; / / ch3 is the target array and the space must be larger than ch2 printf ("% s\ n", my_strcat (ch3, ch2)) / / chain access to return 0;}
Running result
At this point, the study of "how to implement strcpy and strcat functions in C language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.