To be more familiar with the functions of string operations, the corresponding functions are accumulated as follows:
(1) sprintf () function: writes formatted data to a string.
Currently, sprintf functions are most used for conversion of various types into strings.
Header file: stdio. h
Function prototype: int sprintf (char * buffer, const char * format, [argument]… );
Return Value: String Length (strlen)
In addition to the fixed types of the first two parameters, you can take over multiple parameters later. But its essence is obviously in the second parameter:
On the formatted string, both printf and sprintf use the formatted string to specify the string format, and some format specifiers (format specifications) starting with "%" are used inside the format string) to occupy a location, and provide the corresponding variables in the Variable Parameter List at the end, the function will replace the specifier with the variable at the corresponding location to generate a string that the caller wants.
One of the most common applications of sprintf is to print Integers to strings. Therefore, sprintf can replace itoa in most cases.
// Print the integer 123 into a string and save it in s.
Sprintf (s, "% d", 123); // generate "123"
You can specify the width. If the width is insufficient, spaces are filled on the left:
Sprintf (s, "% 8d % 8d", 123,456 7); // generate: "123 4567"
Of course, you can also align left:
Sprintf (s, "%-8d % 8d", 123,456 7); // generate: "123 4567"
You can also print the data in hexadecimal format:
Sprintf (s, "% 8x", 4567); // lowercase hexadecimal notation, with 8 width positions and right alignment
Sprintf (s, "%-8X", 4568); // in hexadecimal notation, the width occupies 8 positions and is left aligned.
In this way, the hexadecimal string of an integer is easy to obtain, but when printing the hexadecimal content, we usually want an equal-width format with 0 on the left, what should we do? Simply add 0 to the number that represents the width.
(2) strlen () function used to obtain the character array or String Length
This function is relatively simple and mainly used to obtain the length of an array or a string.
(3) strncat () function: concatenates two strings.
Prototype: extern char * strncat (char * dest, char * src, int n );
Usage: # include <string. h>
Function: add the first n characters of the string referred to by src to the end of dest (overwrite '\ 0' at the end of dest) and add' \ 0 '.
Note: The memory areas specified by src and dest cannot overlap and dest must have sufficient space to accommodate src strings.
Returns the pointer to dest. Www.2cto.com
(4) strcat () function: concatenates two strings.
Function prototype: extern char * strcat (char * dest, char * src );
Usage: # include <string. h>
Function: add the src string to the end of dest (overwrite '\ 0' at the end of dest) and add' \ 0 '.
Note: The memory areas specified by src and dest cannot overlap and dest must have sufficient space to accommodate src strings.
Returns the pointer to dest.
Author: liangxanhai