C language str-xxx string library function processing Daquan [super detailed]

C language str series library functions are often called by the interviewer to write the implementation of a certain function on paper during the interview. The implementation of the library function is included in the header file <string.h>. There are the following methods

1 strcpy

#include <string.h>
char *strcpy(char *str1, const char *str2);
copy the string str2 (including '\0') to the string str1, and return str1.

2 strncpy

#include <string.h>
char *strncpy(char *str1, const char *str2, size_t count);
Copy up to count characters in the string str2 to the string str1, and return str1. If there are less than count characters in str2, then fill with '\0' until count characters are satisfied.

3 broken

#include <string.h>
char *strcat(char *str1, const char *str2);
copy str2 (including '\0') to the end of str1 (connection), and return str1. The '\0' that terminates the original str1 is overwritten by the first character of str2.

4 strncat

#include <string.h>
char *strncat(char *str1, const char *str2, size_t count);
Connect up to count characters in str2 to the end of str1, terminate str1 with '\0', and return str1. The '\0' that terminates the original str1 is overwritten by the first character of str2.
Note that the maximum number of characters copied is count+1.

5 strcmp

#include <string.h>
int strcmp(const char *str1, const char *str2);
Comparing two strings in lexicographical order, the meaning of the returned integer value is as follows:
less than 0, str1 is less than str2;
equal to 0, str1 is equal to str2;
greater than 0, str1 is greater than str2 ;

6 strncmp

#include <string.h>
int strncmp(const char *str1, const char *str2, size_t count);
Same as strcmp, except compares at most count characters. The integer value returned according to the comparison result is as follows:
less than 0, str1 is less than str2;
equal to 0, str1 is equal to str2;
greater than 0, str1 is greater than str2;

7 str

#include <string.h>
char *strchr(const char *str, int ch);
Returns a pointer to the first occurrence of the character ch in the string str, or returns NULL if ch is not included in str.

8 Strrchr

#include <string.h>
char *strrchr(const char *str, int ch);
Returns a pointer to the last occurrence of the character ch in the string str, or NULL if ch is not included in str.

9 strspn

#include <string.h>
size_t strspn(const char *str1, const char *str2);
returns the length of the first substring in the string str1 composed of the characters in the string str2.

10 strcspn

#include <string.h>
size_t strcspn(const char *str1, const char *str2);
returns the length of the first substring in the string str1 that is not composed of characters in the string str2.

11 strpbrk

#include <string.h>
char *strpbrk(const char *str1, const char *str2);
returns a pointer to the position where any character in the string str2 first appears in the string str1; if str1 does not have the same character as str2, then return NULL.

12 strstr

#include <string.h>
char *strstr(const char *str1, const char *str2);
Returns a pointer to the position where the string str2 first appears in the string str1; if str1 does not contain str2, returns NULL.

13 str

#include <string.h>
size_t strlen(const char *str);
returns the length of the string str, '\0' is not included.

14 strerror

#include <string.h>
char *strerror(int errnum);
returns a pointer to the error message string corresponding to the error number errnum (the specific content of the error message depends on the implementation).

15 strtok

#include <string.h>
char *strtok(char *str1, const char *str2);
Search str1 for the word delimited by the delimiter in str2.
A series of calls to strtok() will break the string str1 into words delimited by the characters in str2. When str1 is not empty for the first call, it searches str1 for the first word consisting of characters not in str2, replaces the next character in str1 with '\0', and returns a pointer to the word. Each subsequent strtok() call (the parameter str1 is replaced by NULL), starts after the previous end position, and returns the next word composed of characters other than those in str2. Returns NULL when there is no such word in str1. The string str2 can be different for each call.
like:

char *p;
p = strtok("The summer soldier,the sunshine patriot", " ");
printf("%s", p);
do {
    p = strtok("\0", ", "); /* 此处str2是逗号和空格 */
    if (p)
        printf("|%s", p);
} while (p);

The displayed result is:The | summer | soldier | the | sunshine | patriot

16 memcpy

#include <string.h>
void *memcpy(void *to, const void *from, size_t count);
Copy count characters from from to to. and return to.

17 memmove

#include <string.h>
void *memmove(void *to, const void *from, size_t count);
The function is similar to memcpy, the difference is that when objects overlap, the function can still be executed correctly.

18 memcmp

#include <string.h>
int memcmp(const void *buf1, const void *buf2, size_t count);
Compare the first count characters of buf1 and buf2, the return value is the same as that of strcmp.

19 memchr

#include <string.h>
void *memchr(const void *buffer, int ch, size_t count);
Returns a pointer to the first occurrence of ch in buffer, or returns NULL if no match is found in the first count characters of buffer.

20 memset

#include <string.h>
void *memset(void *buf, int ch, size_t count);
Replace the first count characters in buf with ch, and return buf.

21 Convert numbers to strings

包含在头文件: #include <stdlib.h>

● itoa(): Convert an integer value to a string.
● ltoa(): Converts a long integer value to a string.
● ultoa(): Converts an unsigned long integer value to a string.
● gcvt(): Convert the floating-point number to a character string and round off.
● ecvt(): Convert double-precision floating-point values ​​to strings, and the conversion result does not include decimal points.
● fcvt(): The specified number of digits is the conversion precision, and the rest are the same as ecvt().

21 Convert string to number

包含在头文件: #include <stdlib.h>

● atof(): Converts a string to a double-precision floating-point value.
● atoi(): Convert a string to an integer value.
● atol(): Converts a string to a long value.
● strtod(): Converts a string to a double-precision floating-point value and reports any remaining numbers that could not be converted.
● strtol(): Converts a string to a long value and reports any remaining numbers that could not be converted.
● strtoul(): Converts a string to an unsigned long value and reports any remaining numbers that could not be converted.

22 character processing functions

包含在头文件: #include <ctype.h>

int isalpha(c) c is an alphabetic character
int isdigit(c) c is a numeric character
int isalnum(c) c is an alpha or numeric character
int isspace(c) c is space, tab, newline
int isupper(c) c is capital letter
int islower(c) c is a lowercase letter
int iscntrl(c) c is the control character
you sprint(c) c is a printable character, including spaces
int isgraph(c) c is a printable character, excluding spaces
int isxdigit(c) c is a hexadecimal digit character
int ispunct(c) c is for punctuation
int tolower(int c) When c is an uppercase letter, return the corresponding lowercase letter, otherwise return c itself
int toupper(int c) When c is a lowercase letter, return the corresponding uppercase letter, otherwise return c itself

Note: These functions return a non-zero value when the condition is true.

ps: Add math function:

包含在头文件: #include <math.h>

Trigonometric functions:

Trigonometric functions sin cos tan
inverse trigonometric functions asin acos for the purpose
hyperbolic function born cosh fishy

Exponential and logarithmic functions:

Exponential function with base e exp
natural log function log
base 10 logarithmic function log10

Other functions:

square root sqrt
absolute value fabs
Exponentiation, the first argument is the base, the second is the exponent double pow(double, double)
The remainder of a real number, the two parameters are the dividend and the divisor double fmod(double, double)

The following functions return double precision values ​​(including functions ceil and floor). In the following table, except for the parameters specified in it, the other parameters of all functions are of double type.

function prototype meaning interpretation
ceil(x) Find the smallest integer not less than x (returns the double value corresponding to this integer)
floor(x) Find the largest integer not greater than x (returns the double value corresponding to this integer)
atan2(y, x) Find tan-1(y/x), the range of its value is [-pai,pai]
ldexp(x, int n) find x*2n
frexp(x, int *exp) Decompose x into y 2n, which is a decimal located in the interval [1/2,1), which is returned as the function result, and the integer n is returned through the pointer exp (an int variable address should be provided). Both results have the value 0 when x is 0
modf(x, double*ip) Decompose x into a fractional part and an integer part, the fractional part is used as the return value of the function, and the integer part is returned by the pointer *ip.

Guess you like

Origin blog.csdn.net/qq_40587575/article/details/128358404