[C language] String library functions and partial simulation implementation

Directory

foreword

1. Find the length of the string

1, strlen – find the length of the string

2. String functions with unlimited length

1, strcpy – copy string

2, strcat – string append

3, strcmp – compare strings

3. Length-restricted string functions

1. strncpy – copy count strings

2. strncat – add count to string

3, strncmp – string comparison count

Four, string search

1. strstr – Find whether there is another string in the string

2, strtok – split string

5. Error message reporting

1. strerror

Six, character operation

Seven, memory operation function

1. memcpy

2. memmove

3. memset

4. memcmp

8. Summary


Foreword

The operation of C language to process characters and strings is complicated, and it is very troublesome to complete various operations only by one’s own code, but mastering some library functions of C language can simplify one’s own code and achieve the goal more quickly, and master library functions proficiently Not only to know its meaning, but also to understand its reason.

1. Find the length of the string

1, strlen – find the length of the string

① Function understanding

The strlen function returns the number of characters before ‘\0’ (not including ‘\0’ ), so the parameter must end with ‘\0’. The return value of the strlen function is size_t, which is unsigned.

②Simulation implementation

Implemented by creating a temporary variable

int my_strlen(const char* str)
{
int count = 0;
//When *str is not '\0', str shifts to the right, count + 1
while (*str)
{
count + + ;
str++;
}
return count;
}

No temporary variable creation (recursive) implementation

int my_strlen(const char* str)
{
//When *str is not '\0', str shifts to the right, return value + 1
if (*str != '\0')
return 1 + my_strlen(str + 1);
else
return 0;
}

2. String functions with unlimited length

1, strcpy – copy string

① Function understanding

The strcpy function will copy the ‘\0’ (including ‘\0’) of the source string to the target space, so the source string must end with ‘\0’, and the target space must Large enough to hold the source string.

②Simulation implementation

char* my_strcpy(char *dest, const char *scr)
{
//Assign the content of *scr to *dest, and move the two pointers to the right until '\0' appears
while (*dest + + = *scr + + )
{
;
}
return dest;
}

2, strcat–string appending

① Function understanding

The source string must end with ‘\0’, and the target space must be large enough (the space other than itself can accommodate the content of the source string) and can be modified.

②Simulation implementation

char* my_strcat(char *dest, const char *scr)
{
// point dest to '\0'
while (*dest)
dest + + ;
//dest appends scr until it hits '\0'
while (*dest = *scr)
{
dest + + ;
scr++;
}
return dest;
}

3, strcmp – compare strings

① Function understanding

Compare the character ASCII code values of the two strings, if they are different, stop, if string1 is large, return a value >0, if string1 is small, return a value <0, otherwise until '\0', return '0 ' means equal.

②Simulation implementation

int my_strcmp(const char* str1, const char* str2)
{
// Check if the string is empty
    assert(str1 & amp; & amp; str2);
// Compare characters of two strings
while (*str1 == *str2)
{
//If it is '\0' at the same time, return equal
if ((*str1 == '\0') & amp; & amp; (*str2 == '\0'))
return 0;
str1++;
str2++;
}
//If there is a difference, return the difference between the two characters at this time
return *str1 - *str2;
}

3. Length-restricted string functions

1, strncpy – copy count strings

① Function understanding

Copy count strings from the source string to the target space. If the source string is less than count, add 0 after copying the source string until count strings.

②Function realization

char* my_strncpy(char* dest, const char* scr, size_t count)
{
assert(dest & amp; & amp; scr);
//Copy count characters
while (count--)
{
/ / Determine whether the scr string is copied
if (*scr != '\0')
{
*dest = *scr;
dest + + ;
scr++;
}
else
{
*dest = '0';
dest + + ;
}
}
return dest;
}

2, strncat – add count to string

① Function understanding

Append count characters, if the source string is not enough, just append the source string

②Simulation implementation

char* my_strncat(char* dest, const char* scr, size_t count)
{
assert(dest & amp; & amp; scr);
//dest points to '\0'
while (*dest)
dest + + ;
//Append count characters or append all characters of scr
while (count-- & amp; & amp; *scr)
{
*dest = *scr;
dest + + ;
scr++;
}
return dest;
}

3, strncmp–string comparison count

①Function understand

Compare count strings, and stop if they are both ‘\0’ or not equal.

②Simulation implementation

int my_strncmp(const char* str1, const char* str2, size_t count)
{
while (count--)
{
if (*str1 == '\0' & amp; & amp; *str2 == '\0')
return 0;
if (*str1 == *str2)
{
str1++;
str2++;
}
else
{
return *str1 - *str2;
}
}
return 0;
}

4. String search

1, strstr–Find whether there is another string in the string

① Function understanding

Finds if another string exists within a string.

②Simulation implementation

char* my_strstr(const char* str1, const char* str2)
{
// string to be searched
char* search = str1;
// Shift the searched string to the right each time
while (*search)
{
char* sc = search;
char* cp = str2;//The string reset for each search
while (*sc == *cp)
{
sc ++ ;
cp++;
if (*cp == '\0')
return search;
}
search + + ;
}
// return null if not
return NULL;
}

2, strtok – split string

Precautions:

(1) The sep parameter is a string that defines the set of characters used as separators
(2) The first parameter specifies a string containing 0 or more tokens separated by one or more delimiters in the sep string.
(3) The strtok function finds the next mark in str, ends it with \0, and returns a pointer to this mark. (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. )
(4) 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.
(5) 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 mark
remember.
(6) If there are no more tokens in the string, a NULL pointer is returned.

5. Error message report

1, strerror

Return the error code and the corresponding error message.

6. Character operation

Function If its parameters meet the following conditions, it will return true
iscntrl any control character
isspace Blank characters: space ‘ ‘, form feed ‘\f’, line feed ‘\\
‘, carriage return ‘\r’, Tab ‘\t’ or vertical tab ‘\v’
isdigit

Decimal number 0~9

isxdigit

Hexadecimal numbers, including all decimal numbers, lowercase letters a~f, uppercase letters A~F

islower Lowercase letter a~ z
isupper capital letters A~Z
isalpha Letter a~z or A~Z
isalnum

Letter or number, a~z,A~Z,0~9

ispunct Punctuation, any Graphic characters that are not numbers or letters (printable)
isgraph any graphic character
isprint any Printable characters, including graphic characters and blank characters

7. Memory operation function

1, memcpy

① Function understanding

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 it encounters ‘\0’. If the source and destination overlap in any way, the result of the copy is undefined.

②Simulation implementation

void* my_memcpy(void* dest, void* src, size_t num)
{
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*) dest + 1;
src = (char*)src + 1;
}
}

2, memmove

① Function understanding

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.

②Simulation implementation

void* my_memmove(void* dest, void* src, size_t num)
{
if (src > dest)
{
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*) dest + 1;
src = (char*)src + 1;
}
}
else
{
while (num--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
}

3, memset

memset is an initialization function, the function is to set all the memory in a certain block to the specified value.

  1. dest points to the block of memory to be filled.
  2. c is the value to be set.
  3. count is the number of characters to be set to the value.
  4. The return type is a pointer to storage dest.

4, memcmp

memcmp compares num bytes starting from ptr1 and ptr2 pointers

The return value is as follows:

8. Summary

The above is the understanding and application of some string library functions. There are many useful functions in the library function of C language. If you are proficient in the code, it will become a great help in writing code. As a novice who is new to C language, the above is only my personal understanding. I hope that students who are interested in library functions will be helpful.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge