1, strlen() function
The strlen() function counts the length of a string.
#include <stdio.h> #include <string.h> int main() { char *p = "hello world"; int len = strlen(p); printf("p strlen[%d]\n", len); return 0; }
2, strcat() function
strcat() is used to splice strings, receive two strings as functions, attach the backup of the second string to the end of the first string, and take the new string formed after splicing as the first string, and the second string remains unchanged. The type of strcat() function is char * (that is, the pointer to char), and returns the address of the first parameter, that is, the address of the first string after splicing the second string.
#include <stdio.h> #include <string.h> int main() { char ch1[20] = "Hello "; char ch2[10] = "World"; //char *ch3; printf("strcat front: ch1[%s],ch1 Pointer[%p],ch2[%s],ch2 Pointer[%p]\n", ch1, &ch1, ch2, &ch2); char *ch3 = strcat(ch1,ch2); printf("strcat After: ch1[%s],ch1 Pointer[%p],ch2[%s],ch2 Pointer[%p],ch3[%s],ch3 Pointer[%p]\n", ch1, &ch1, ch2, &ch2, ch3, &ch3); printf("strcat(ch1,ch2)[%p]\n", strcat(ch1,ch2)); return 0; }
3, strncat() function
The strcat() function cannot check whether the first array can hold the second string. If the space allocated to the first array is not large enough, the extra characters will overflow to the adjacent storage units.
#include <stdio.h> #include <string.h> int main() { char ch1[20] = "Hello "; char ch2[10] = "World"; printf("strincat front: ch1[%s],ch1 Pointer[%p],ch2[%s],ch2 Pointer[%p]\n", ch1, &ch1, ch2, &ch2); char *ch3 = strncat(ch1,ch2,10); printf("strncat After: ch1[%s],ch1 Pointer[%p],ch2[%s],ch2 Pointer[%p],ch3[%s],ch3 Pointer[%p]\n", ch1, &ch1, ch2, &ch2, ch3, &ch3); printf("strncat(ch1,ch2)[%p]\n", strncat(ch1,ch2,10)); return 0; }
4, strcmp() function
The strcmp() function compares the contents of two strings, not the address of the string. If it is the same, it returns 0. If it is different, it returns a non-0 value. According to the ASCII code comparison, the difference between the ASCII codes of the two parameters is returned.
#include <stdio.h> #include <string.h> int main() { char ch1[10] = "A"; char ch2[10] = "A"; char ch3[10] = "a"; int i1 = strcmp(ch1,ch2); int i2 = strcmp(ch1,ch3); printf("strcmp(ch1,ch2)=[%d]\n", i1); printf("strcmp(ch1,ch3)=[%d]\n", i2); return 0; }
4, strncmp() function
The strcmp() function compares the characters in the string until no characters are found. This process may end at the end of the string. The strncmp() function can compare different characters, or it can only compare the number of characters specified by the third parameter (the nth bit of the first parameter and the second parameter).
5, strcpy() function and strncpy() function
strcpy() copies the second string parameter to the first string parameter. What is returned is the value of the first parameter, that is, the address of the first parameter.
The strncpy() function is n,) copies the first n-bit string of the second string parameter to the first string parameter.
6, sprintf() function
The sprintf() function is declared in stdio h. Not in string h.
sprintf() function and printf() function types. sprintf() function writes data to a string instead of printing on the display. This function can synthesize multiple elements into a string.
7, Other functions
1,char *strchr(const char *s, int c);
If the s string contains a c character, the function returns a pointer to the first position of the s string (the null character at the end is also part of the string and is also within the search range). If it is found, it returns a null pointer.
2,char *strpbrk(const char *s1, const char *s2);
If the s1 character contains any character in the s2 string, the function returns a pointer to the first position of the s1 string. If no character of the s2 string is found in the s1 string, it returns a null pointer.
3,char *strrchr(const char *s, int c);
This function returns the s string to find the last position of the c character (the null character at the end is also part of the string and is also within the search range). If it is found, it returns a null pointer.
4,char *strstr(const char *s1, const char *s1)
This function returns a pointer to the first position of s2 string in s1 string. If s2 is not found in s1, it returns a null pointer.
5,size_t strlen(const char *s);
This function returns the number of characters in a string, excluding null characters at the end.