Simulation implementation of strcpy, strcat, strstr, memcpy and memmove

Table of Contents

Introduction:

1.strcpy function

1.1The function and basic usage of strcpy function

1.2 Note:

1.3 Simulation and implementation of strcpy function

2. strcat function

2.1The function and basic usage of strcat function

2.2 Note:

2.3 Simulation and implementation of strcat function

3.strstr function

3.1 The function and basic usage of strstr function

3.2 Simulate the strstr function

4.memcpy function

4.1The role and basic usage of the memcpy function

4.2 Note:

4.3 Simulation and implementation of memcpy function

5.memmove function

5.1The role and basic usage of the memmove function

5.2 Note:

5.3 Simulate the implementation of memmove function

5.3.1 Note:

5.3.2 Code implementation:


Introduction:

strcpy strcat and strstr are string functions

memcpy and memmove are memory functions

In this article we will talk about the use of these functions and how to simulate them yourself.

There are also comments within the code of each simulation implementation

1.strcpy function

The role and basic usage of the 1.1strcpy function

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

strcpy is the abbreviation of stringcopy, which means “string copy function”. Its function is to copy the string pointed by source to the function pointed by destination.

1.2 Note:

  • The source string must end with ‘\0’.
  • ‘\0’ in the source string will be copied to the target space.
  • The target space must be large enough to accommodate the source string.
  • The target space must be variable.

1.3 Simulate the strcpy function

char* my_strcpy(char* dest, const char* src)
{
char* ret = dest;//Save the starting address of the target space
while ((*dest + + = *src + + ))
{
;
}
return ret;//return target
}
int main()
{
char arr1[100] = { 0 };
char arr2[] = "Hello world!";
my_strcpy(arr1, arr2);
printf("%s\\
", arr1);
return 0;
}

2.strcat function

The role and basic usage of the 2.1strcat function

strcat – string concatenation function, the general form is as follows:

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

The calling form strcat (character array 1, character array 2) connects the source string to the target string, and puts the result in the target character array. After the function is called, a function value-the address of the target string-is obtained.

2.2 Note:

  • The source string must end with ‘\0’.
  • The target space must be large enough to accommodate the contents of the source string.
  • The target space must be modifiable.

2.3 Simulation and implementation of strcat function

char* my_strcat(char* dest, const char* src)
{
char* ret = dest; //Record the starting address of the target string
while (*dest != '\0')//Enter the loop first, until it reaches the end of its own character content
{
dest + + ;
}
while (*dest + + = *src + + )//Assign src dereference to dest, including '\0'
{
;
}
return ret;//Return the starting address of the target string
}
int main()
{
char arr1[100] = "Hello";
char arr2[] = "world!";
my_strcat(arr1, arr2);
printf("%s\\
", arr1);
return 0;
}

3.strstr function

The role and basic usage of the 3.1strstr function

The strstr function is used to find the first occurrence of another string in a string and return a pointer to that position.

char * strstr (const char *str1, const char * str2);

str1 is the string to be searched, str2 is the string to be searched.

If str2 is an empty string, return str1

If str2 does not appear in str1, NULL is returned.

3.2 Simulate the strstr function

const char* my_strstr(const char* str1, const char* str2)
{
const char* cp;//record the starting matching position
const char* s1;//Traverse str1
const char* s2;//Traverse str2
if (*str2 == '\0')//If str2 is empty, return directly
{
return str1;
}
cp = str1;//points to the starting position of str1
while (*cp)
{
s1 = cp;
s2 = str2;
while (*s1 & amp; & amp; *s2 & amp; & amp; *s1 == *s2)//*s1 *s2 does not point to \0, s1 = s2 enters the loop
{
s1 + + ;//Add later to start traversing
s2++;
}
if (*s2 == '\0')//After the loop ends, if s2 is '\0', the surface has been found
{
return cp;
}
cp + + ;//After the loop ends, if s2 is not '\0', it means it has not been found yet, and cp increases by one.
}
return NULL;
}
int main()
{
char arr1[] = "abbbcd";
char arr2[] = "bbc";
char* ret = my_strstr(arr1, arr2);
if (ret == NULL)
{
printf("Not found\\
");
}
else
{
printf("%s\\
",ret);
}
return 0;
}

4.memcpy function

The role and basic usage of the 4.1memcpy function

It is to copy the data of one memory area to another memory area. It can be used to copy complex data types such as structures and arrays.

void * memcpy ( void * destination, const void * source, size_t num );

destination represents the address of the target memory area, source represents the address of the source memory area, and num represents the number of bytes to be copied.

4.2 Note:

  • The target memory area and the source memory area cannot overlap, otherwise the results will be unpredictable.
  • The number of bytes to be copied, num, cannot exceed the effective length of the target memory area and the source memory area, otherwise an out-of-bounds access error will occur.
  • This function starts from the memory address pointed to by src and copies num bytes backward to the beginning of the memory address pointed by destination.

4.3 Simulation and implementation of memcpy function

void* my_memcpy(void* dest, void* src, size_t sz)
{
while (sz--)
{
*(char*)dest = *(char*)src;//Since the memcpy function copies in bytes
//The int type is four bytes, so it needs to be converted to the char* type and copied byte by byte.
dest = (char*)dest + 1;//Forced types are temporary and will not change the type forever
src = (char*)src + 1;//So it still needs to be forced to char* again
}
}
int main()
{
int arr1[10] = { 0 };
int arr2[] = { 0,9,2,6 };
my_memcpy(arr1, arr2, 16);//16 bytes, which is 4 int types
int i = 0;
for (i = 0; i < 4; i + + )
{
printf("%d ", arr1[i]);
}
return 0;
}

5.memmove function

The role and basic usage of the 5.1memmove function

Its function is to move a certain number of bytes in the memory area and return a pointer to the target memory area.
Similar to memcpy function

void * memmove ( void * destination, const void * source, size_t num );

destination represents the pointer to the target memory area, source represents the pointer to the source memory area, and num represents the number of bytes to be moved.

5.2 Note:

  • 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 target space overlap, you have to use the memmove function.

5.3 Simulation and implementation of memmove function

5.3.1 Note:

Because during the move, the previous value may be moved to the back, changing the value of the overlapping memory.
By the time the contents of the overlapping memory are moved back, the original value of the area has changed.
The initial value loss leads to different results than expected, so here we need to distinguish between situations

5.3.2 Code Implementation:

void* my_memmove(void* dest, const void* src, size_t sz)
{
void* ret = dest; //Record the starting address of the target string

//Such as moving the memory pointer at the back to the front
//The movement trajectory is from front to back, first change the content in front of the target memory, and then change the following memory in sequence
if (dest < src)
{
while (sz--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
//If you move the previous memory area to the later memory area
//The movement trajectory is from back to front, first change the content behind the target memory, and then change the previous content in sequence.
else
{
while (sz--)
{
*((char*)dest + sz) = *((char*)src + sz);
}
}
return ret;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
my_memmove(arr, arr + 1, 16);//Count 4 elements starting from the 2nd element and move it to the memory with subscript 0
int i = 0;
for (i = 0; i < 9; i + + )
{
printf("%d ", arr[i]);
}
return 0;
}