In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "C language string function and memory function", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "C language string function and memory function how to use" article.
Strlen
Gets the string length.
Strlen-size_t strlen (const char * string)
1. The string ends with'\ 0', and the strlen function returns the number of characters that precede'\ 0' in the string (excluding'\ 0').
two。 The string pointed to by the parameter must end with'\ 0'.
3. Note that the return value of the function is size_t, which is unsigned (error prone)
Strlen simulation implementation
/ / size_t strlen (const char* string); # include#includesize_t my_strlen (const char* str) {assert (str! = NULL); int count = 0; while (* str) {count++; str++;} return count;} int main () {int len = my_strlen ("abcedfg") Printf ("% d\ n", len); return 0;} strcpy
Copy a string
Strcpy-char * strcpy (char * strDestination, const char * strSource)
1. The source string must end with'\ 0'.
two。 The'\ 0' in the source string is copied to the destination space.
3. The target space must be large enough to ensure that the source string is held.
4. The target space must be variable.
Simulation implementation of strcpy
# include#includechar* my_strcpy (char* dest, const char* src) {assert (dest & & src); char* ret = dest; / / copies the string pointed to by src into the space pointed to by dest, including'\ 0' while (* dest++ = * src++); / / returns the starting address return ret of the destination space } int main () {/ / char* arr1 = "abcdefghi"; / / the err target space must be changeable char arr1 [] = "abcdefghi"; / / char arr2 [] = {'haggling source string must end with'\ 0' char arr2 [] = "hello"; my_strcpy (arr1, arr2); printf (arr1) Return 0;} strcat
String append
Strcat-char * strcat (char * strDestination, const char * strSource)
1. The source string must end with'\ 0'.
two。 The target space must be large enough to hold the contents of the source string.
3. The target space must be modifiable.
4. You can't add to yourself.
Simulation implementation of strcat
# include#includechar* my_strcat (char* dest, char* src) {char* ret = dest; assert (dest & & src); / / 1. Find the'\ 0' while (* dest) dest++; / / 2 in the destination string. Append while (* dest++ = * src++); return ret;} int main () {char arr1 [37] = "hello"; char arr2 [] = "world"; my_strcat (arr1, arr2); / / append arr2 printf ("% s\ n", arr1) after arr1; return 0;} strcmp
String comparison
Strcmp simulation implementation
# include#includeint my_strcmp (const char* str1, const char* str2) {assert (str1 & & str2); while (* str1 = = * str2) {if (* str1 = ='\ 0') {return / equivalent} str1++; str2++ } return * str1-* str2;} int main () {char* p1 = "abcdef"; char* p2 = "abqjf"; int ret = my_strcmp (p1, p2); printf ("% d\ n", ret); return 0;} strncpy
Copies characters from one string to another.
Strncpy-char * strncpy (char * strDest, const char * strSource, size_t count)
1. Copy count characters from the source string to the destination space.
two。 If the length of the source string is less than count, after copying the source string, append 0 to the destination until count.
Strncpy simulation implementation
Char* my_strncpy (char* dest, const char* src, size_t count) {char* ret = dest; while (count & & (* dest++ = * src++)) {count--;} if (count) {while (--count) {* dest++ ='\ 0' }} return ret;} int main () {char arr1 [20] = {0}; char arr2 [] = "hey"; my_strncpy (arr1, arr2, 6); printf (arr1); return 0;} strncat
The additional character of the string.
Strncat-char * strncat (char * Dest, const char * Source, size_t count)
Strncat simulation implementation
Char* my_strncat (char* dest, const char* src, size_t count) {char* ret = dest; while (* dest++); dest--; while (count--) {if (! (* dest++ = * src++)) {return ret } * dest ='\ 0mm; return ret;} int main () {char arr1 [37] = "hello"; char arr2 [] = "world"; my_strncat (arr1, arr2, 7); puts (arr1); return 0;} strncmp
Compare the characters of two strings.
Strncmp-int strncmp (const char * string1, const char * string2, size_t count)
1. Compare to the occurrence of a different character or the end of a string or the end of all count strings.
Strstr
Look for substrings
Strstr-char * strstr (const char * string, const char * strCharSet)
1. Each function returns a pointer to the first occurrence of strCharSet in string, or NULL if strCharSet does not appear in string. If strCharSet points to a string of length 0, the function returns string.
Strstr simulation implementation
# include#includechar* my_strstr (const char* str1, const char* str2) {assert (str1 & & str2); char* cur = (char*) str1; char* S1, * S2; if (! * str2) return ((char*) str1); while (* cur) {S1 = cur; S2 = (char*) str2 While (* S1 & & * S2 & &! (* S1-* S2)) {S1 return cur; cur++;;} if (! * S2) return cur; cur++;} return NULL } int main () {char* p1 = "aqqqcdef"; char* p2 = "qqc"; char* ret = my_strstr (p1, p2); if (ret = = NULL) printf ("string does not exist\ n"); else printf ("% s\ n", ret); return 0;} strtok
Cut the string to find the next token in the string
Strtok-char * strtok (char * str, const char * sep)
The 1.sep parameter is a string that defines a collection of characters to use as delimiters.
two。 The first parameter specifies a string that contains 0 or more tokens separated by one or more delimiters in the sep string.
The 3.strtok function finds the next tag in str, ends it with'\ 0', and returns a pointer to that tag. (note: the strtok function changes the string being manipulated, so use the
The string sliced by the strtok function is generally a temporary copy and can be modified.
The first argument to the 4.strtok function is not NULL, the function will find the first tag in the str, and the strtok function will save its position in the string.
The first argument to the 5.strtok function is NULL, and the function starts at the location where it was saved in the same string, looking for the next tag.
6. If no more tags exist in the string, the NULL pointer is returned.
Int main () {char arr [] = "hyr@baidu.com"; char* p = "@."; char buf [1024] = {0}; strcpy (buf, arr); char* ret = NULL; for (ret = strtok (arr, p); ret! = NULL;ret = strtok (NULL, p)) {printf ("% s\ n", ret) } return 0;} strerror
Get the system error message (strerror) or print the error message provided by the user.
Strerror-char * strerror (int errnum)
1. Returns the error message corresponding to the error code.
2.errno is a global error code variable. When an error occurs in the library function of C language during execution, the corresponding error code will be assigned to errno.
# include#include#include// must include header files int main () {FILE* pf = fopen ("unexist.ent", "r"); if (pf = = NULL) {printf ("Error opening file unexist.ent:%s\ n", strerror (errno)); / / errno: Last error number return 0;} / /. Return 0;} tolower\ toupper
Int tolower (int c);-convert uppercase letters to lowercase letters
Int toupper (int c);-convert lowercase letters to uppercase letters
Memcpy
Copy characters between buffers
Memcpy-void * memcpy (void * dest, const void * source, size_t num)
1. The function memcpy copies num bytes of data back from the location of source to the memory location of destation.
two。 This function does not stop when it encounters\ 0.
3. If there is any overlap between source and destion, the result of replication is undefined.
Memcpy simulation implementation
# include#includestruct Stu {char name [20]; int age;}; void* my_memcpy (void* dest, const void* src, size_t num) {assert (dest & & src); void* ret = dest; while (num--) {* ((char*) dest) = * ((char*) src); + (char*) dest + + (char*) src;} return ret;} int main () {struct Stu arr1 [] = {{"zhangsan", 13}, {"lisi", 17}}; struct Stu arr2 [3] = {0}; my_memcpy (arr2, arr1, sizeof (arr1)); return 0;} memmove
Move one buffer to another
Memmove-void * memmove (void * dest, const void * src, size_t count)
1. The difference with memcpy is that the source and target memory blocks handled by the memmove function can overlap.
two。 If the source and target spaces overlap, you have to use the memmove function to handle it.
Memmove simulation implementation
# include#includevoid* my_memmove (void* dest, const void* src, size_t count) {assert (dest & & src); void* ret = dest; if (dest < src) {while (count--) {* (char*) dest = * (char*) src; + + (char*) dest + + (char*) src;}} else {while (count--) {* ((char*) dest + count) = * ((char*) src + count);}} return ret } int main () {int arr [] = {1 int arr [] = {1 [] = {1 [] = {1 * $$5 * } return 0;} memcmp
Compare characters between two buffers
Memcmp-int memcmp (const void * buf1, const void * buf2, size_t count)
1. Compare count bytes starting with buf1 and buf2 pointers
Memset
Sets the buffer to the specified character
Menset-void * memset (void * dest, int c, size_t count)
The above is about the content of this article on "how to use C language string function and memory function". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.