Detailed explanation of common string APIs in C language

The following are notes, sorry for the haste, first introduce the functions used in the string chapter, and then introduce a few common APIs


  1. malloc function prototype: void *malloc(size_t size)
    The C library function void *malloc(size_t size) allocates the required memory space and returns a pointer to it

  2. free The C library function void free(void *ptr) releases the memory space allocated by calling calloc, malloc or realloc before release
    Function: 1 release, prevent memory leak 2 prevent dangling pointer (a kind of wild pointer)
    Ordinary variables and arrays are in Open up space on the stack, which can be automatically recycled; while malloc opens up space on the heap, and it will be released after the end of the program. Generally, free is required.

  3. realloc function prototype void *realloc(void *ptr, size_t szie);
    expansion function, the C library function void *realloc(void *ptr, size_t szie) tries to recall the
    memory block pointed to by ptr allocated by calling malloc or calloc before Size
    void * realloc(original address, increased memory);

  4. memset is an initialization function, the function is to set all the memory in a certain block to the specified value.
    void *memset(void *str, int c, size_t n);
    s points to the memory block to be filled.
    c is the value to be set.
    n is the number of characters to be set to the value.
    The return type is a pointer to storage s.

  5. assert function prototype void assert(int expression);
    the function of assert is to calculate the expression expression. If its value is false (that is, 0),
    then it first prints an error message to stderr, and then terminates the program by calling abort.
    The disadvantage of using assert is that frequent calls will greatly affect the performance of the program and increase additional overhead.
    After debugging, you can disable the assert call by inserting #define NDEBUG before the statement containing the #include

API

  1. output string:
  • puts();
  • printf(“%s”,p);
  1. Get the string:
  • scanf(“&s”,p);
  • gets; function prototype char * gets(char *str); whether the memory is legal
    The gets function can be read infinitely, and it is prone to overflow. If it overflows, the extra characters will be written to the stack
    which overwrites the original contents, destroying the value of one or more unrelated variables
  1. Calculate the length:

strlen function
prototype: size_t strlen( const char *string );

char arr[20] = “abcdef”;
int len = strlen(arr);
printf(“%d”, len);

Note:
The string needs to end with \0, and the strlen function returns the number of characters that appear before \0 in the string (not including \0). The
string pointed to by the parameter must end with \0. Pay
attention to the return of the function The value type is size_t type
More reference sizeof and strlen Are you really clear?

  1. copy:
  • strcpy function prototype char *strcpy(char *dest, comst char *src);
  • strncpy function prototype char *strncpy(char *dest, const char *src, int n);
    means to copy the first n bytes starting from the src address in the string pointed to by src to the function array pointed to by dest,
    and Return the copied dest
  • memcpy function prototype void *memcpy(void *destin, void *source, unsigned n); function: the function memcpy copies n characters from the object pointed to by source to the object pointed to by destin return value: the function memcpy returns the pointer of destin.
  1. Stitching:
  • strcat function prototype char *strchar(char *dest, const char * src); copy the string pointed to by src
    (including '\0') to the end of the string pointed to by dest (delete the '\0' at the original end of dest ), to ensure that *dest is long enough
    to accommodate the copied *src, the original characters in *src remain unchanged, and the return value points to a pointer to dest
  1. Compare:
  • strcmp function prototype int strcmp(const char*s1, const char *s2);
    if str1 = str2, return 0; if str1<str2, return a negative number; if str1>str2, return a positive number
  • strncmp function prototype int strncmp (const char*s1, const char *s2, size_t n);
    the function is to compare str1 and str2, at most compare the first n bytes, if the first n bytes of str1 and str2 are the same, return 0; If str1>str2, return a number greater than 0
    If str1<str2, return a number less than 0
  1. Find :
  • strchr function: char * strchr() const char * str, char ch);
    Function:
    find a character in the string
    Parameters:
    str - the string to be retrieved
    ch - the character to be found
    Return value:
    the position where ch first appears , if no ch is found return NULL
  • strstr function char *strstr(char *str1, const char *str2);
    function:
    find the substring in the string
    Parameters:
    str1 - the string to retrieve
    str2 - the substring to look for
    Return value:
    if str2 is a substring of str1 string, returns the address of the first occurrence of str2 in str1; if str2 is not a substring of str1, returns NULL
  1. Split:
  • String split strtok function, char * strtok(char * str, const char * delim);
    Function:
    Decompose the string str into a set of strings
    Parameters:
    str - the string delim to be split
    - delimiter
    Return value:
    start from str The beginning of a split string. Returns NULL when there is no split string.
  1. Convert size:
  • Convert to uppercase strupr function
    char *strupr(char *str);
    parameter: str //string to be converted
    Function: used to convert the characters in the string to uppercase
    and return the converted lowercase string, which is str.
  • Convert to lowercase strlwr function
    char *strlwr(char *str);
    function is equivalent to strlpr function

Guess you like

Origin blog.csdn.net/qq_44333320/article/details/125716294