table of Contents
I. Introduction
Both series of functions are included in the header file string.h, it is often used for data operation in the array. In most cases, str family of operating an array of characters, mem series of operations to other types of arrays .
Two: str Series
There are frequently used
strlen: Statistics string length (number before the end of the null symbol)
strcpy: used to copy a string to an array of characters
strcmp: compare two character strings size
strcat: string used to connect the two
The following use cases in detail these functions
2.1 strlen
Function prototype is
size_t strlen( char *str );
Function: Returns the length of string str
It can be understood as unsigned int size_t
Sizeof strlen and where the difference can be seen in this article https://blog.csdn.net/qq_39112646/article/details/102673836
2.2 strcpy
Function prototype is
char *strcpy(char * to , const char * from)
Function: copy the string from the character string to characters, including null terminator and return a pointer to a value of
2.3 strcmp
Function prototype is
int strcmp ( const char * str1 , const char * str2 )
Function: string comparison str1 and str2 string (a character corresponding to a comparison ascii code)
str1 < str2 返回 -1
str1 = str2 返回 0
str1 > str2 返回 1
printf("%d\n",strcmp("how","hello")); //1
printf("%d\n",strcmp("how","how")); //0
printf("%d\n",strcmp("hello","how")); //-1
2.4 strcat
Function prototype is
char * strcat(char * str1 ,char * str2 )
Function: function to be connected to the end of the string str2 str1, and returns a pointer str1
int main(){
char p[20] = "hello ";
char *q = "world";
strcat(p,q);
printf("%s\n",p); //hello world
printf("%s\n",q); //world
return 0;
}
Wherein there is a corresponding strncpy strncmp strncat
2.5 strncpy
Function prototype is
char *strncpy( char *to, const char *from, size_t count )
Function: Copy string from at most count characters of the string to the,
If the string is from less than count, the remainder of a '\ 0' padding
Returns the string to complete the process
2.6 strncmp
Function prototype is
int strncmp( const char *str1, const char *str2, size_t count )
Function: To compare the strings str1 and str2 string at most count characters
Which returns the type of results as strcmp
If the length of the parameter string of any one of COUNT is less than, then when comparing to the first '\ 0', the comparison is terminated
2.7 strncat
Function prototype is
char *strncat( char *str1, const char *str2, size_t count )
Function: connecting string str up to count characters of the string in str1, adding '\ 0'
2.8 strchr
Function prototype is
char * strchr()const char * str, char ch)
Function: The function returns the position of the first occurrence of ch in str point, or NULL if not found ch
int main(){
char p[] = "0123456789";
char *temp = strchr(p, '0'); //0123456789
char *temp = strchr(p, '8'); //89
printf("%s\n",temp);
}
Three: mem series
3.1 memset
Function prototype is
void * memset(void * buffer , int ch , size_t count)
Function: function to copy ch buffer count characters from scratch, the buffer pointer and returns
Memset () may be used for some memory initialized to a value, e.g.
memset(the_array, '\0', sizeof( the_array) )
int main(){
int p[10] = {1,2,3,4,5,6};
memset(p,'\0',sizeof(p)); //重新将数组初始化为0
}
3.2 memcpy
Function prototype is
void * memcpy( void * to, const void * from , size_count)
Functions: Functions copied from the from the count to the character to return to the pointer. If to and from overlap, the behavior is undefined
3.3 memcmp
Function prototype is
int memcmp( const void *buffer1, const void *buffer2, size_t count );
Function: function compares the first count and buffer1 buffer2 characters, the return value and strncmp () similar
3.4 memchr
Function prototype is
void * memchr(const * void * buffer, int ch ,size_t count)
Function: function to find the position of the first occurrence of the string ch count characters of the buffer pointed to by the array
Returns a pointer to the location of the first occurrence of ch in the string, if ch is not found, returns NULL
int main(){
char *p= "hello world";
char *q = memchr(p,'w',10); //world
printf("%s",q);
}
3.5 memmove
Function prototype is
void *memmove( void *to, const void *from, size_t count );
Function: memcpy the same, and different from that as to overlap the normal function