<string. h> There are many commonly used string functions in the library. Mengxin blogger has made some summary here
catalogue
1.strlen
Function prototype: size_t strlen ( const char * str );
Function: calculate string length
Parameter: str string to calculate
Return value: the length of the returned string does not include '\ 0'
Example:
int main() { char str[] = "abcdef"; printf("%d\n", strlen(str)); //The result is 6 return 0; }
Simulation Implementation:
//Counter method int my_strlen1(const char *str) { int count = 0; assert(str); while (*str++) { count++; } return count; } //Pointer minus pointer int my_strlen2(const char *str) { char *p = str; assert(str); while (*str) str++; return str - p; } //recursion int my_strlen2(const char *str) { assert(str); if (*str != '\0') { return 1 + my_strlen(str + 1); } else { return 0; } }
2.strcpy
Function prototype: char* strcpy(char * destination, const char * source);
Function: copy a string to another place
Parameter: Source: the source of the string to be copied; destination the location where the string to be copied is placed;
Return value of the address of the completed copy of the string:
Note: the space where the string will be placed should be larger than the string to be copied
Example:
int main() { char str1[10] = { 0 }; char str2[] = "abcdef"; printf("%s\n", strcpy(str1, str2)); //The result is abcdef return 0; }
Simulation Implementation:
char* my_strcpy(char *dest, const char *src) { char *tmp = dest; assert(dest && src); while (*src) { *dest = *src; src++; dest++; } return tmp; }
3.strcat
Function prototype: char * strcat (char * destination, const char * source);
Function: copy a string to the end of another string
Parameter: source is the string to be copied, and destination is copied to the end of the string
Return value: returns the address after the copy is completed
Example:
int main() { char str1[10] = "abc"; char str2[] = "def"; printf("%s\n", strcat(str1, str2)); //Results abcdef return 0; }
Simulation Implementation:
char* my_strcat(char *dest, const char *src) { char *tmp = dest; assert(dest && src); while (*dest)//Find end dest++; while (*src) { *dest++ = *src++; } return tmp; }
4.strcmp
Function prototype: int StrCmp (const char * STR1, const char * STR2);
Function: string comparison, compare characters according to ASCII code table
Parameters: str1,str2 two strings to be compared
Return value: str1 greater than str2 returns a number greater than 0: str1 equals str2 returns 0;str1 less than str2 returns a number less than 0
Example:
int main() { char str1[] = "abcd"; char str2[] = "acde"; int tmp = strcmp(str1, str2); if (tmp > 0) { printf("str1 > str2\n"); } else if (tmp < 0) { printf("str1 < str2\n"); } else { printf("str1 = str2\n"); } //Result STR1 < STR2 return 0; }
Simulation Implementation:
int my_strcmp(const char *dest, const char *src) { assert(dest && src); while (*dest == *src && *dest) { dest++; src++; } return *dest - *src; }
5.strncpy
Function prototype: char * strncpy (char * destination, const char * source, size_t Num);
Function: copy n characters from the source string to the target space
Parameter: destination copy destination source copy source num number of copied characters
Return value: returns the address after the copy is completed
Example:
int main() { char str1[] = "abcdefg"; char str2[10] = { 0 }; printf("%s\n", strncpy(str2, str1, 3)); //Results abc return 0; }
6.strncat
Function prototype: char * strncat (char * destination, const char * source, size_t Num);
Function: copy n characters from the source to the end of the destination and add '\ 0'
Parameters: destination copy destination, source copy source, num number of copies
Return value: returns the address after the copy is completed
Example:
int main() { char str1[10] = "abc"; char str2[] = "defgh"; printf("%s\n", strncat(str1, str2, 3)); //Results abcdef return 0; }
7.strncmp
Function prototype: int strncmp (const char * STR1, const char * STR2, size_t Num);
Function: compare different characters or the end of a string or all n characters
Parameters: str1,str2 two strings to compare num number of characters to compare
Return value: str1 greater than str2 returns a number greater than 0: str1 equals str2 returns 0;str1 less than str2 returns a number less than 0
Example:
int main() { char str[][5] = { "R2D2" , "C3PO" , "R2A6" }; int n; puts("Looking for R2 astromech droids..."); for (n = 0; n < 3; n++) if (strncmp(str[n], "R2xx", 2) == 0) { printf("found %s\n", str[n]); } //result: //found R2D2 //found R2A6 return 0; }
8.strstr
Function prototype: char * strstr (const char * STR1, const char * STR2);
Function: String lookup function to find string str2 in string str1
Parameters: str1 string to be searched, str2 string to be searched
Return value: returns the address where the same location is found
Example:
//Example 1 int main() { char str1[] = "hello whord!"; char str2[] = "ell"; printf("%s", strstr(str1, str2)); //Result ello who! return 0; } //Example 2 int main () { char str[] ="This is a simple string"; char * pch; pch = strstr (str,"simple"); strncpy (pch,"sample",6); puts (str); //This is a simple string return 0; }
9.strtok
Function prototype: char * strtok (char * STR, const char * SEP);
Function: find the separator flag in the string, and then split it
Parameter str is the string to be searched. The string of sep separator can be multiple separators
Return value: the first call returns the address of the first segment of string. When the str parameter is NULL, it returns the address of the next segment of string. When the string ends, it returns NULL
Supplement:
Example:
int main() { char str1[] = "hell#o wh$ord"; char str2[] = "#$"; char* pstr = NULL; for (pstr = strtok(str1, str2); pstr; pstr = strtok(NULL, str2)) { printf("%s\n", pstr); } //result //hell //o wh //ord return 0; }
10.strerror
Function prototype: char * strError (int errnum);
Function: return error information according to the error code
Parameter: ermun error code
Return value: the address of the error message
Supplement: strerror function is used with the global variable errno
Errno header file < errno h>
printf("error message: %s", strerror(errno));
11.memcpy
Function prototype: void * memcpy (void * destination, const void * source, size_t Num);
Memory copy function
Parameter destination copy destination source copy source num copy bytes
Return value: returns the address of the destination of void * type
Examples
int main() { int a[] = { 1, 5, 6, 848, 4, 11, 3, 51, 9 }; int b[20] = { 0 }; int i = 0; memcpy(b, a, sizeof(int) * 8); for (i = 0; i < 20; i++) { printf("%d ", b[i]); } //Results 1 56 848 4 11 3 51 0 0 0 0 0 0 0 0 0 0 0 0 0 return 0; }
Simulation Implementation:
void* my_memcpy(void* dest, const void* src, size_t n) { void* tmp = dest; assert(dest); assert(src); while (n--) { *(char*)dest = *(char*)src; (char*)dest += 1; (char*)src += 1; } return tmp; }
12.memmove
Function prototype: void * memmove (void * destination, const void * source, size_t Num);
Function: the function is the same as memmove, but it can handle string overlap and replace memcpy
Parameter: destination copy destination source copy source num copy bytes
Return value: returns the address of the destination of void * type
Simulation Implementation:
void* my_memmove(void* dest, const void* src, size_t n) { void* tmp = dest; assert(dest); assert(src); if (dest < src)//Copy from front to back { while (n--) { *(char*)dest = *(char*)src; src = (char*)src + 1; (char*)dest += 1; } } else { while (n--) { *((char*)dest + n - 1) = *((char*)src + n - 1); } } return tmp; }
13.memcmp
Function prototype: int memcmp (const void * PTR1, const void * ptr2, size_t Num);
Function: memory comparison function, compare by bytes,
Parameter: ptr1 ptr2 two memory addresses to be compared num bytes to be compared
Return value: if ptr1 is greater than ptr2, return a number greater than 0: if ptr1 is equal to ptr2, return 0;ptr1 less than ptr2 returns a number less than 0