Summary of commonly used string functions---2

There are more than 20 functions for processing strings in the ANSI C library. Below are some commonly used functions.

char *strcpy(char *restrict s1,const char *restrict s2)

This function copies the string pointed to by s2 (including null characters) to the position pointed to by s1, and the return value is s1.

char *strncpy(char *restrict s1,const char *restrict s2, size n);

This function copies the string pointed to by s2 to the position pointed by s1, the number of copied characters does not exceed n, and its return value is s1. This function does not copy the characters after the null character, if the source string contains less than n characters The target string ends with the copied null character. If the source string has n or more than n characters, the null character is not copied.

char *strcat(char *restrict s1,const char *restrict s2);

This function copies the string pointed to by s2 to the end of the string pointed to by s1. The first character of the s2 string will overwrite the null character at the end of the s1 string, and the function returns s1.

char *strncat(char *restrict s1,const char *restrict s2, size n);

This function copies n characters in the s2 string to the end of the s1 string. The first character of the s2 string will overwrite the empty character at the end of the s1 string, and will not copy the empty character in the s2 string and the following , And add a null character at the end of the copied character, return s1;

int strcmp(const char *s1,const char *s2)

If the s1 string is behind the s2 string in the machine sorting sequence, this function returns a positive number, if it is equal, it returns 0, if the s1 string is before the s2 string in the machine sorting sequence, it returns a negative number

int strncmp(const char *s1,const char *s2, size n)

This function stops the comparison after encountering the first null character after comparing n characters

char *strchr(const char *s,int c)

If the s string contains the c character, this function returns a pointer to the first position of the s string, if not found, it returns a null pointer.

char *strpbrk(const char *s1,const char *s2);

If the s1 character contains any character in the s2 string, this function returns a pointer to the first position of the s1 string, otherwise it returns a null pointer.

char *strrchr(const char *s,int c)

This function returns the position of the last occurrence of the c character in the s string, or a null character if not found

char *strstr(const char *s1,const char *s2)

This function returns a pointer to the first position of the s2 string in the s1 string. If s2 is not found in s1, it returns a null pointer.

size_t strlen(const char *s);

This function returns the number of characters in the s string, excluding the null character at the end.

Guess you like

Origin blog.csdn.net/qq_42392049/article/details/112691727