Advanced C language-string functions

Advanced C language-string functions

All are in the header file " #include<string.h> ".
1. The strlen function-measures the length of a string, and stops when it encounters'\0'. The return value is an unsigned integer.
Function prototype: size_t strlen( const char *string );
function parameter: character type pointer (that is, the first address of the string)-char *
2. strcpy function-string copy.
Function parameters (2): the address of the destination string (strDestination) and the address of the source string (strSource), the return value is the address of the destination string.
Function prototype: char *strcpy( char *strDestination, const char *strSource );
3. strcmp function-string comparison. The return value is 1, 0, -1
function parameters (2): 1 string address and 2 string address.
When string 1>string 2, return 1;
when string 1<string 2, return -1;
when string 1=string 2, return 0;
function prototype: int strcmp( const char *string1, const char *string2 );
4. strcat function-string concatenation function. Concatenate two strings.
Function parameters (2): the address of the destination string (strDestination) and the address of the source string (strSource), the return value is the address of the destination string.
Function prototype: char *strcat( char *strDestination, const char *strSource );
5. strncpy function-string copy function with length limitation.
Function parameters (3): the address of the target string (strDest), the address of the source string (strSource), and the number of characters to be copied. The return value is the address of the target string.
Function prototype: char *strncpy( char *strDest, const char *strSource, size_t count );
6. strncat function-string concatenation function with length limitation.
Function parameters (3): the address of the target string (strDest), the address of the source string (strSource), and the number of characters to be connected. The return value is the address of the target string.
Function prototype: char *strncat( char *strDest, const char *strSource, size_t count );
Example:

char string[80] = "This is the initial string!";
 char suffix[] = " extra text to add to the string...";
 strncat( string, suffix, 19 );//自动补‘\0’

The result is: "This is the initial string! extra text to add"
7. strncmp function-string comparison function with length limitation.
Function parameters (3): 1 string address, 2 string address and the number of characters to be compared, the return value is 1, 0, -1.
When string 1>string 2, return 1;
when string 1<string 2, return -1;
when string 1=string 2, return 0;
function prototype: int strncmp( const char *string1, const char *string2, size_t count );
8. strstr function-substring search function.
Function parameters: main string address and substring address, the return value is the address of the first character corresponding to the first time the substring is found in the main string.
Function prototype: char *strstr( const char *string, const char *strCharSet );//The former is the main string, and the latter is the substring.
9. strtok function-string split function.
Function parameters: the address of the divided character string and the delimiter string. But the principle is to replace the corresponding position of the separator in the string with'\0'.
Function prototype: char *strtok( char *strToken, const char *strDelimit );
Note: When strToken is NULL, it will continue to find the position of the last division.
So there is a fixed format:

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char *token;
 token = strtok( string, seps );
   while( token != NULL )
   {
    
    
      printf( " %s\n", token );
      token = strtok( NULL, seps );
   }

The final result is:
A
string
of
tokens
and
some
more
tokens
10. The memcpy function-can copy any type of data, but the internal copy is byte by byte. (Memory overlap may occur)
Function parameters (3): (any type) destination string (dest) address, (any type) source string (src) address and the number of bytes to be copied , no return value.
Function prototype: void *memcpy( void *dest, const void *src, size_t count );
11. memmove function-similar to memcpy function, there will be no memory overlap.
Function parameters (3): (any type) destination string (dest) address, (any type) source string (src) address and the number of bytes to be copied , no return value.
Function prototype: void *memmove( void *dest, const void *src, size_t count );

Guess you like

Origin blog.csdn.net/qq_45841205/article/details/110094873