当前位置:网站首页>C language -- string operation function and memory operation function
C language -- string operation function and memory operation function
2022-07-27 06:23:00 【Autumn mountain chariot God AE】
String manipulation functions
strlen- Find string length function
Realization principle : Traversal string Until the end of the string is found '\0',.
Simulation implementation code :
unsigned int mystrlen(const char* arr)
{
int count = 0;
assert(arr != NULL);
while (*arr)
{
arr++;
count++;
}
return count;
}strcpy- String copy function
Copy the contents of one string to another
Realization principle : Traverse every character of the source string , And copy it to the target string , Until traversing to '\0'
Simulation implementation code :
char* mystrcpy(char* dst, const char* sou)
{
char* tmp = dst;
assert(dst != NULL && sou != NULL);
while (*dst++ = *sou++) // When traversing to '\0' No longer enter the cycle
{
;
}
return tmp;
}strcmp- String comparison function
Compare the contents of each string in the string , When the same , Continue to the next character , If it's all the same return 0.
If a character larger than , Return is greater than the 0 The integer of , Less than . Back to less than 0 The integer of
Simulation implementation code
int mystrcmp(const char* arr1, const char* arr2)
{
assert(arr1 != NULL && arr2 != NULL);
while (*arr1 == *arr2)
{
if (*arr1 == '\0')
{
return 0;
}
arr1++;
arr2++;
}
return *arr1 - *arr2;
}strcat- Insert one string into the end of another string
Realization principle , First traverse the target string , Until the starting pointer points to '\0'
From target string '\0' Position start , Insert the character of the source string , Until traversing to the source string '\0' until .
Simulation implementation code :
char* mystrcat(char* arr1, const char* arr2)
{
char* tmp = arr1;
assert(arr1 != NULL && arr2 != NULL);
while (*arr1)
{
arr1++;
}
while (*arr1++=*arr2++)
{
;
}
return tmp;
}Memory manipulation function
memcpy- Byte copy function
Of the source address count Bytes are copied to the destination address .
Realization principle , Cast two addresses to char* type , In this way, the step size of solution application and self increment in the future is 1 Bytes , Then start with the destination address and the original address , The address starts to increase in steps of one byte , Dereference the destination address and source address , Each byte is copied to the byte at the source address .
Simulation implementation code :
char* mymemcpy(void* arr1, const void* arr2, size_t count)
{
char* ret = (char*)arr1;
while (count--)
{
*(char*)arr1 = *(char*)arr2;
arr1=(char*)arr1+1;
arr2=(char*)arr2+1;
}
return ret;
}memmove- Byte shift function
You can copy the byte contents of two overlapping addresses .
Realization principle When the starting address of the target space is smaller than the starting address of the source space or the two parts do not overlap , It should be copied from front to back , In this way, the unreplicated part will not be overwritten during the replication process .
When the starting address of the target space is larger than the starting address of the source space , It should be copied from back to front .
Simulation implementation code ;
char* mymemove(void* arr1, const void* arr2, size_t count)
{
char* ret = (char*)arr1;
if (arr1 <= arr2 || arr1>=(char*)arr2+count)
{
while (count--)
{
*(char*)arr1 = *(char*)arr2;
arr1 = (char*)arr1 + 1;
arr2 = (char*)arr2 + 1;
}
}
else
{
while (count--)
{
*((char*)arr1 + count) = *((char*)arr2 + count);
}
}
return ret;
}边栏推荐
猜你喜欢
随机推荐
ROS workspace coverage
多坐标变换
Automatic tracking
wireshark IP地址域名解析
Pzk's first understanding of pointer in learning C language
Chapter for software testing
Non photorealistic rendering (NPR) paper understanding and reproduction (unity) - stylized highlights for cartoon rendering and animation
Dynamic planning for solving problems (6)
Brief introduction to unity menu interface
ROS通信机制进阶
Random points in non overlapping rectangle (force deduction daily question)
多线程的相关知识
ROS运行管理之launch文件
Install Wireshark correctly
Li Kou 236. the nearest common ancestor of binary tree
The principle of hash table and the solution of hash conflict
Programming learning records - Lesson 4 [branch and loop statements]
Force deduction problem solving monotonous stack
wireshark图形界面介绍
Learning the operation environment needs to be equipped during software testing









