Use of library functions and precautions

Character functions and string functions

Library function link: link .
Focus on the use and precautions of library functions that handle characters and strings

Find the string length
strlen

Unlimited length string function
strcpy
strcat
strcmp

Introduction to string functions with restricted length
strncpy
strncat
strncmp

String search
strstr
strto

Error message report
strerror

Function introduction

strlen:

size_t strlen (const char * str);

The string has'\0' as the end marker, and the strlen function returns the number of characters that appear before the'\0' in the string (not including'\0').
The string pointed to by the parameter must end with'\0'.
Note that the return value of the function is size_t, which is unsigned (error-prone).
Learn the analog implementation of the strlen function

strcpy

char* strcpy(char * destination, const char * source);

Copies the C string pointed by source into the array pointed by destination, including the terminating null
character (and stopping at that point). The
source string must end with'\0'.
The'\0' in the source string will be copied to the target space.
The target space must be large enough to ensure that the source string can be stored.
The target space must be variable.
Learn to simulate

strcat

char * strcat ( char * destination, const char * source);

Concatenate the source string to the target string The
source string must end with '0'.
The target space must be large enough to accommodate the contents of the source characters. The
target space must be modifiable

strcmp

int strcmp ( const char * str1, const char * str2 );

The standard stipulates: if the
first string is greater than the second string, a number greater than 0 will be returned
. If the first string is equal to the second string, then 0 will be returned if the
first string is less than the second string, then it will be returned. Numbers less than 0

strncpy

char * strncpy ( char * destination, const char * source,size_t num);

Copy num characters from the source string to the target space.
If the length of the source string is less than num, after copying the source string, add 0 to the end of the target until num

strncat and the same

char * strncat ( char * destination,const char * source,size_t num);

strncmp

 int strncmp(const char * str1,const char * str2, size_t num );

The comparison shows that another character is different or a string ends or num characters are all compared.

strtok

char * strtok ( char * str, const char * sep );

The sep parameter is a string that defines the set of characters used as a separator. The
first parameter specifies a string that contains 0 or more tokens separated by one or more separators in the sep string.
The strtok function finds the next token in str, ends it with \0, and returns a pointer to this token. (Note: The strtok function will change
the string being manipulated, so the string split by the strtok function is generally a temporary copy and can be modified.)
The first parameter of the strtok function is not NULL, the function will find The first mark in str, strtok function will save its position in the string.
The first parameter of the strtok function is NULL, the function will start at the saved position in the same string and look for the next token.
If there are no more tokens in the string, a NULL pointer is returned.

Guess you like

Origin blog.csdn.net/weixin_50843868/article/details/109787241