C Language - Summary of String Operation Functions

        In the library functions of C, there are abundant string manipulation functions, and the flexible use of these library functions in the usual coding will achieve twice the result with half the effort

One: str series

char *strcpy(s, ct) Copy the string ct (including '\0') to the string s, and return s. It is necessary to pay attention to whether the length of s can accommodate ct.
char *strncpy(s, ct, n)

Copies up to n characters of the string ct to the string s, and returns s. If there are less than n characters in ct, pad with '\0'

If n is less than the length of ct, that is, the characters in the copied part of ct, there is no \0, which needs to be added manually

In strncpy, the length specifies how many characters will be written into the target character array. If the source string is longer than the specified length, the string will not end with \0 bytes.

char *strcat(s, ct) Concatenate the string ct to the end of s, and return s. The overlapping result of src and dst is undefined, and the remaining space of the target character array must be enough to accommodate the entire source string
char *strncat(s, ct, n)

Connect up to the first n characters of the string ct to the end of the string s, and end with '\0'; return s

The length parameter of the strncat function specifies the maximum number of characters copied from the source string, but its result is always terminated by a \0 byte.

int *strcmp(cs, ct) Compare the strings cs and ct; when cs<ct, return a negative number; when cs==ct return 0; when cs>ct return a value greater than 0
int *strncmp(cs, ct,n) Compare the strings cs and ct to sign n characters; when cs<ct, return a negative number; when cs==ct return 0; when cs>ct return a value greater than 0
char *strchr(cs, c) Returns a pointer to the first occurrence of the character c in the string cs; if c is not included in cs, the function returns NULL
char *strrchr(cs, c) Returns a pointer to the last occurrence of the character c in the string cs, if c is not included in cs. returns NULL
size_t strcspn(str1, str2) Retrieve the consecutive characters at the beginning of the string str1 that do not contain the characters of the string str2
size_t strlen(str) Calculates the length of the string str up to but not including the null-terminated character
char *strpbrk(str, c) Retrieves the first character in the str string that matches the character in str2, returns the position (excluding \0)
size_t strspn(str1, str2) Retrieves the subscript of the first character in the string str1 that does not appear in the string str2
char *strstr(str1, str2) Find the first occurrence of str2 in str1
char *strtol(str, share) Split a string by delim

        In the above functions, some appear in pairs such as strcpy and strncpy, strcmp and strncmp. The name of the two functions is just a letter difference. The latter is added with an n, because the former is under certain conditions It is unsafe, we generally call it unsafe function, the appearance of the latter is to make up for the hidden dangers caused by unsafe functions, but the former has been used for many years, and it is not easy to change on the original basis, so another a function.

        Let's take strcpy and strncpy as examples to see why strcpy is unsafe

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    // please write your code here
    char *myName = "ftz";

    char oriarr[5] = {0};
    strcpy(oriarr,myName);
    printf("enough space:%s\n",oriarr);

    char oriarr2[2] = {0};
    strcpy(oriarr2,myName);
    printf("not enough space:%s\n",oriarr2);
   
}

result:

        It can be seen that when the space of the destination string is insufficient, the source string is still copied, and the memory behind the destination string is stepped on, which will cause unpredictable errors.

        A parameter n is added to strncpy, and the caller can flexibly set the number of bytes to be copied. In addition, the description of the function also reminds the caller what to do to ensure safety.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    // please write your code here
    char *myName = "ftz";

    //strncpy(s, ct, n) normal
    char oriarr[5];
    strncpy(oriarr,myName,4);
    printf("normal:%s\n",oriarr);

    //strncpy(s, ct, n) ct中少于n个字符
    char oriarr2[5];
    strncpy(oriarr2,myName,5);
    printf("ct中少于n个字符:%s\n",oriarr2);

    //strncpy(s, ct, n)  n小于ct的长度  需要手动添加\0
    char oriarr3[5];
    strncpy(oriarr3,myName,2);
    oriarr3[2] ='\0';
    printf("n小于ct的长度:%s\n",oriarr3);
   
}

result:

 Two: mem series

void *memchr(void *str, int c, size_t n)  Searches for the first occurrence of the character  c (an unsigned character) in the first n bytes  of the string pointed to by  the parameter  str . Returns NULL if no character appears
int memcmp(str1, str2, n) Compare the first n bytes of str1 and str2.

If the return value < 0, it means that str1 is smaller than str2.

If the return value > 0, it means that str1 is greater than str2.

If the return value = 0, it means that str1 is equal to str2.

void *memcpy(dst, src, n) Copy n bytes from src to dst and return a pointer to dst
void *memmove(dst, src, n) Copy n bytes from src to dst and return a pointer to dst. Copying correctly also when overlapping
void *memset(str, c, n) Copy the character c to the first n bytes of the string pointed to by the parameter str

        Personally, the frequency of using the mem series in coding is higher than that of the str series. When using the functions of the mem series, we need to pay special attention to the protection of the last parameter n to ensure that the memory we want to operate is absolutely safe, only for our goals. memory to operate.

Three: Comparison of the two series

1, memchr and strchr

memchr detects a piece of memory, and strchr checks a string;

strchr will stop at \0 but memchr will not;

memchr will also be more efficient;

2, memcmp and strcmp

Feature comparison:

Both can be used to compare strings, but there is a big difference between the two

strcmp is compared according to the byte (byte-wise), and will check whether there is a "\0" terminator during the comparison process. Once any string pointer encounters the terminator during the forward process, the comparison will be terminated;

The memcmp function is used to compare whether the contents of two memory blocks are equal. It is usually used to test whether the strings are equal when used for string comparison, and byte-wise string comparison is not often performed;

Best if the objects to be compared contain some "holes" caused by spaces filled in struct objects due to boundary alignment requirements, extra spaces at the end of unions, unused parts of string allocated space cannot use memcmp;

Efficiency difference:

 The string compared by strcmp, strcmp needs to check whether it has encountered the \0 character at the end of the string

memcmp compares memory blocks, no need to check whether there is \0

So memcmp is more efficient than strcmp

3. memcpy and strncpy

Both have no protection against memory overlap, the difference is:

When the number of bytes copied is less than or equal to the length of the src string, the two are equivalent; when the number of bytes copied is greater than the length of the src string, strncpy uses \0 to fill the number of bytes, and memcpy will continue according to the address Duplicated, will cause malfunction.

strncpy copies the content according to the specified length of the string, and will terminate when encountering 0; memcpy will not terminate.

4. memcpy and memmove

When the memory partially overlaps, memmove guarantees that the result of the copy is correct, but memcpy does not guarantee that the result of the copy is correct.

The difference between memmove and memcpy (ABD)

A. When the memory partially overlaps, memmove guarantees that the result of the copy is correct, but memcpy does not guarantee that the result of the copy is correct

B. When the src memory area overlaps with the dest memory area and the area where dest is located is before the area where src is located, memcpy can correctly copy

C. When the src memory area overlaps with the dest memory area and the area where src is located is before the area where dest is located, memcpy can correctly copy

D. For BC, memmove can be copied correctly

The correct description of memcpy and memmove is [Only consider the execution result, not the standardization] C

A. When the source and destination addresses overlap, only memmove can be used

B. When the source and destination addresses do not overlap, only memcpy can be used

C. The source and destination addresses overlap, and when the source > destination, only memmove can be used

D. When the source and destination addresses overlap, and the source < destination, only memmove can be used

 

Guess you like

Origin blog.csdn.net/qq_27071221/article/details/132173571