In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of common library function simulation in C language, which is very detailed and has certain reference value. Interested friends must read it!
Preface
The processing of characters and strings is very frequent in C language, but C language itself has no string type, and strings are usually placed in constant strings or character arrays. The string constant applies to string functions that do not modify it.
Function to introduce strlen (find the string length)
Size_t strlen (const char * str)
The string has'\ 0' as the closing flag, 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'.
The return value of the function is size_t, which is unsigned.
Simulation implementation:
# includesize_t my_strlen (char* arr) {int count = 0; while (* arr) {arr++; count++;} return count;} int main () {char arr [] = "abcdef"; size_t ret=my_strlen (arr); printf ("% u\ n", ret); return 0;} strcpy (string copy)
Char* strcpy (char* destination, const char* source)
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.
Simulation implementation:
# include#includechar * my_strcpy (char* arr,const char* arr1) {assert (arr & & arr1); char* ret = arr; while (* arr++ = * arr1++) {;} return ret;} int main () {char arr [] = "xxxxxxxxxxxxxxx"; char arr1 [] = "abcd"; my_strcpy (arr, arr1) Printf ("% s\ n", arr); return 0;} strcat (string append)
Char * strcat (char * destination, const char * source)
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.
Simulation implementation:
# include#includechar* my_strcat (char* dest, const char* src) {assert (dest & & src); / / assertion-protects both dest and src pointers from null char* ret = dest; / / saves the first address of the target string / / finds the end of the target string\ 0 while (* dest) {dest++ } / / append the source string to the target string until\ 0 while (* dest++ = * src++) {;} return ret;} int main () {char arr [20] = "abc"; char arr1 [] = {'dBBQ. Printf ("% s\ n", my_strcat (arr, arr1)); / / append the contents of the arr1 array to the arr array return 0;} strcmp (string comparison)
Int strcmp (const char * str1, const char * str2)
If the first string is greater than the second string, a number greater than 0 is returned.
If the first string is equal to the second string, 0 is returned.
If the first string is less than the second string, a number less than 0 is returned.
Simulation implementation:
Compare the contents of the string, not the length of the string
# include#includeint my_strcmp (const char* a, const char* b) {assert (a & b); while (* a = = * b) {if (* a = ='\ 0') return 0; return * a-* b } int main () {char a [] = "abc"; char b [] = "abcq"; int ret=my_strcmp (a, b); if (ret > 0) printf (">\ n"); else if (ret= = 0) printf ("=\ n"); else printf ("
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.