Table of contents
1. Introduction of main functions
2. Function details
1、stril
2、strcpy
3、 cracked
4、strcmp
5、strncpy
6、strncat
7、strncmp
8、strstr
9、Strostok
10、strerror
11、 memcpy
12、memove
13、 memcmp
3. The simulation implementation of the most commonly used functions
1、strlen
2、strcpy
3、broken
4、strstr
5、strcmp
6、memcpy
7、memmove
1. Introduction of main functions
1. Find the length of the string
strlen->Find the number of characters before \0 in the string
2. String functions with unlimited length
strcpy->Copy the characters from the source address to the terminator \0 to the target address
strcat->appends a copy of the source string to the destination string
strcmp->Comparing two strings, the first string is greater than the second string, then returns a number greater than 0, the first string is equal to the second string, then returns 0, the first string is less than the second string Two strings, returns a number less than 0
3. Introduction to length-restricted string functions
strncpy->Copy
num
characters from source string to target space
strncat->appends the number of characters specified in the source string to the destination string
strncmp->Compare two strings, but the number of second string comparisons is specified here, and the comparison rules are the same as strcmp above
4. String search
strstr -> find substring
strtok ->
splits a string into tokens
5. Error message report
strerror->return error code, the corresponding error message.
6. Character operation
memory manipulation function
memcpy-> Copy
num bytes of data
backwards from the location of the source address to
the memory location of the destination address.
memmove-> has the same effect as memcpy, but the difference with memcpy is that the source memory block and target memory block processed by the memmove function can overlap
memset ->
fill memory block
memcmp-> Compare num bytes starting from ptr1 and ptr2 pointers
2. Function details
1、stril
原型:size_t strlen ( const char * str );
The string has '\0'
as the end mark, and
the strlen
function returns
the number of characters (not including '\0' ) that appear before '\0
' in the string
.
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
)
2、strcpy
原型:char* strcpy(char * destination, const char * source );
Copies the C string pointed to by source into the array pointed to by destination, including the terminating null character (and stopping at that point).
The source string must be terminated with '\0'.
Will copy the '\0' in the source string to the destination space, and he will return
the address of the array pointed to by destination.
The destination space must be large enough to accommodate the source string.
The target space must be mutable.
3、 cracked
原型:char * strcat ( char * destination, const char * source );
Appends a copy of the source string to the destination string. The terminating null character destination is overwritten by the first character of source and includes a null character at the end of the new string formed by the concatenation of the two in destination. And returns the address of the destination.
The source string must be terminated with '\0'.
The destination space must be large enough to hold the contents of the source string.
The target space must be modifiable.
4、strcmp
原型:int strcmp ( const char * str1, const char * str2 );
This function starts comparing the first character of each string. If they are equal otherwise, it continues with the following pairs until the characters differ or until the end reaches a null character.
standard regulation:
If the first string is greater than the second string, return
a number greater than 0
If the first string is equal to the second string, return 0
The first string is less than the second string, returns a number less than 0
5、strncpy
原型:char * strncpy ( char * destination, const char * source, size_t num );
Copies the first num characters of source to destination. If the end of the source C string (indicated by a null character) is found before num characters are copied, the destination is zero-filled until a total of num characters are written to it.
And he will return
the address of the array pointed to by destination.
Copies num characters from source string to destination 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
.
6、strncat
原型:char * strncat ( char * destination, const char * source, size_t num );
Appends num characters starting from the first of the source to the destination, plus a terminating null character. Returns the address of the array pointed to by destination.
If the length of the C string in source is less than num, only the content up to the end of the copy null character is included.
7、strncmp
原型:int strncmp ( const char * str1, const char * str2, size_t num );
Compare until another character is different or a string ends or all num characters are compared.
8、strstr
原型:char * strstr ( const char *str1, const char * str2);
Returns a pointer to the first occurrence of str1 in str2 , or a null pointer if str2 is not part of str1 .
The matching process does not include the terminating null character, but it stops there.
9、Strostok
原型:char * strtok ( char * str, const char * sep );
The sep parameter is a string that defines the set of characters used as separators
The first parameter specifies a string containing zero
or more
tokens separated by one or more delimiters in the
sep string.
The strtok function finds
the next token in
str , terminates it with \0, and returns a pointer to this token.
(Note: The strtok function will change the string being manipulated, so
the strings split using the
strtok function are generally temporary copied content and can be modified.)
The first parameter of the strtok function is not
NULL
, the function will find
the first mark in
str , and
the 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 search for the next token.
If no more tokens exist in the string, a NULL
pointer is returned.
10、strerror
原型:char * strerror ( int errnum );
Interprets the value of errnum, producing a string containing a message describing the error condition, as if set to errno by the library's functions. The returned pointer points to a statically allocated string, which should not be modified by the program. Further calls to this function may overwrite its contents (no specific library implementation is required to avoid data races). The error string produced by strerror may be specific to each system and library implementation.
11、 memcpy
原型:void * memcpy ( void * destination, const void * source, size_t num );
The function memcpy copies
num bytes of data backwards
from the location of
source to
the memory location of destination .
This function
does not stop when encountering '\0'.
If the source
and
destination
overlap in any way, the result of the copy is undefined.
12、memove
原型:void * memmove ( void * destination, const void * source, size_t num );
The difference
with memcpy is that the source memory block and target memory block processed by
the memmove
function can overlap.
If the source space and the target space overlap, you have to use the memmove
function to deal with it.
13、 memcmp
原型:int memcmp ( const void * ptr1, const void * ptr2, size_t num );
Compare num bytes starting from ptr1
and
ptr2 pointers
The return value is as follows:
If the first string is greater than the second string, return a number greater than 0
If the first string is equal to the second string, return 0
The first string is less than the second string, returns a number less than 0
3. The simulation implementation of the most commonly used functions
1、strlen
int my_len(const char* s)
{
int re = 0;
while (*s)
{
re++;
s++;
}
return re;
}
2、strcpy
char* my_strcpy(char* dest, const char* src)
{
char* ret = dest;
assert(dest != NULL);
while ((*dest++ = *src++));
return ret;
}
3、broken
char* my_strcat(char* s1, char* s2)
{
assert(s1);
assert(s2);
char* re = s1;
while (*s1)
{
s1++;
}
while (*s2)
{
*s1 = *s2;
s1++;
s2++;
}
*s1 = '\0';
return re;
}
4、strstr
char* my_strstr(const char* s1, const char* s2)
{
char *q1, *q2;
char* cp = (char*)s1;
if (!*s2)
return (char*)s1;
while (*cp)
{
q1 = cp;
q2 = (char*)s2;
while (*q1&&*q2&&!(*q1-*q2))
{
q1++;
q2++;
}
if (!*q2)
return cp;
cp++;
}
return NULL;
}
5、strcmp
int my_strcmp(const char* s1, const char* s2)
{
while (*s1 || *s2)
{
if (*s1 > *s2)
return 0;
else if (*s1 == *s2)
{
s1++;
s2++;
}
else
return -1;
}
return 1;
}
6、memcpy
void* my_memcpy(void* st1, const void* st2,size_t num)
{
assert(st1 != NULL);
assert(st2 != NULL);
void* re = st1;
while (num--)
{
*(char *)st1 = *(char *)st2;
st1 = (char*)st1 + 1;
st2 = (char*)st2 + 1;
}
return re;
}
7、memmove
void* my_memmove(void* s1, void* s2, size_t num)
{
void* re = s1;
if (s1 <= s2 || (char*)s1 >= ((char*)s2 + num))
{
while (num--) {
*(char*)s1 = *(char*)s2;
s1 = (char*)s1 + 1;
s2 = (char*)s2 + 1;
}
}
else
{
s1 = (char*)s1 + num - 1;
s2 = (char*)s2 + num - 1;
while (num--) {
*(char*)s1 = *(char*)s2;
s1 = (char*)s1 - 1;
s2 = (char*)s2 - 1;
}
}
return re;
}