string.h is the header file used for string processing in C. It contains the following main functions: copy string: strcpy, strncpy append string: strcat, strncat compare string: strcmp, strncmp find string Length: strlen Search characters: strchr, strrchr
Usage of string.h in C language
string.h is used for characters in C language Standard header files for string processing. It provides a number of functions for manipulating C strings (arrays of characters terminated with '\0').
Main functions
Some of the most commonly used functions in string.h include:
-
strcpy: Copy source string to destination characters String
-
strncpy: Copies up to n characters from the source string to the destination string, padding the rest with '\0'
-
strcat: Append the source string to the end of the target string
-
strncat: Append up to n characters from the source string to the end of the target string
-
strcmp: Compares two strings and returns 0 (equal), a negative number (the source string is smaller than the target string), or a positive number (the source string is larger than the target string)
-
strncmp : Compares the first n characters of two strings, returning 0 (equal), a negative number (the source string is smaller than the target string), or a positive number (the source string is larger than the target string)
-
strlen: Returns the length of the string (excluding '\0')
-
strchr: Searches the string for the first character matching the specified character
-
strrchr: Search for the last character matching the specified character in the string
Usage method
To use the function in string.h, you need Include this header file in your program:
<code class="c">#include <string.h></code>
Copy after login
You can then use functions to manipulate strings. For example, to copy one string into another string, you can use the strcpy function:
<code class="c">char src[] = "Hello";
char dst[5];
strcpy(dst, src);</code>
Copy after login
This copies the contents of the src string into the dst string.
Note
When using the string.h function, you need to pay attention to the following points:
- All string functions assume that the source and target strings have sufficient space to operate.
- The n argument to the strncpy and strncat functions specifies the number of characters to copy or append, excluding '\0'.
- The strcmp and strncmp functions stop comparing when they find the first mismatch in the string.
- strlen function does not include the '\0' character at the end of the string.
The above is the detailed content of Usage of string.h in c language. For more information, please follow other related articles on the PHP Chinese website!