<> Realization of string operation function

1.strlen function
Function function :
Computes the specified string string The length of .
application
int main() { char* str = "abcdef"; printf("%d\n",strlen(str)); return 0; }
Simulation Implementation strlen function
1. Counter implementation
#include<stdio.h> #include<assert.h> int my_strlen(const char*str)
// The length of the string is constant , use const modification { assert(str!=NULL);//assert Assertion int ret = 0; while (*str!=
'\0') { ret++; str++; } return ret; } int main() { char* str = "abcdef"; printf(
"%d\n",my_strlen(str)); return 0; }
2. Recursive implementation
#include<Stdio.h> int my_strlen(const char* str) { assert(str != NULL); if (*
str!= '\0') return 1 + my_strlen(str + 1); else return 0; } int main() { char*
str= "hello bite"; int len = my_strlen(str); printf("%d\n", len); return 0; }
3. Pointer - Pointer implementation
Pointer - The result of the pointer is the number of intermediate elements .
#include<stdio.h> int my_strlen(const char* str) { assert(str != NULL); const
char*start = str;// Storage starting position while (*str != '\0') { str++; } return str - start; }
int main() { char* str = "abcdef"; printf("%d\n",my_strlen(str)); return 0; }
2.strcpy function
Function function : To contain ’\0’ Copy the string of the terminator to another string .
Function prototype
char *strcpy( char *Destination, const char *Source );
Simulation Implementation strcpy function
char* my_strcpy(char*arr1, const char*arr2) { assert(arr1&&arr2); char*ret =
arr1; while (*arr1++ = *arr2++) { ; } return ret; } int main() { char arr1[20] =
"****************"; char arr2[] = "hello bit"; char* ret=my_strcpy(arr1, arr2);
printf("%s\n", ret); return 0; }
3.strcat function
function : Append string
be careful : The additional space is large enough and modifiable
application :
#include<stdio.h> int main() { char arr1[20] = "hello"; char arr2[] = "world";
strcat(arr1, arr2); printf("%s\n", arr1); return 0; }
Simulation Implementation strcat function
#include<stdio.h> #include<assert.h> char *my_strcat(char*dest, const char*src)
{ //1. Find the target space \0 //2. Add assert(dest&&src); char*ret = dest; while (*dest) { dest
++; } while (*dest++ = *src++) { ; } return ret; } int main() { char arr1[20] =
"hello"; char arr2[] = "world";
4.strcmp function
function : Comparison of strings .
be careful : Compared with the corresponding position of the character ASCII Code value, not length .
Simulation Implementation strcmp function
int my_strcmp(const char*s1, const char*s2) { assert(s1&&s2); while (*s1==*s2)
{ if (*s1 == '\0') { return 0; } s1++; s2++; } if (*s1 > *s2) { return 1; } else
{ return -1; } } int main() { char *p = "abcdef"; int ret = my_strcmp("abq",
"abcdef"); printf("%d\n", ret); return 0; }
5.strstr
function : Find a substring in a string .
application :
int main() { char arr1[] = "abcdefghi"; char arr2[] = "bcd"; char *ret=strstr(
arr1, arr2);// stay arr1 Find in arr2 Where the string first appears if (ret != NULL) { printf("%s\n", ret); }
else printf(" String not found \n"); return 0; }
Simulation Implementation strstr function
char *my_strstr(const char*s1, const char*s2) { char*cp = s1; while (*cp) {
char*p1 = cp; char*p2 = s2; while ((*p1!='\0')&&(*p1 == *p2)&&(*p1==*p2)) { p1++
; p2++; } if (*p2 == '\0') { return (char*)cp; } cp++; } return NULL; }
6.strtok
char *strtok( char *strToken, const char *strDelimit );
1)sep The argument is a string , Defines the set of characters used as a separator
(2) The first parameter specifies a string , It contains 0 One or more by sep A mark separated by one or more separators in a string .
(3)strtok Function found str Next tag in , And use it \0
ending , Returns a pointer to the token .( notes :strtok Function changes the string being manipulated , So in use strtok The string cut by function is usually the content of temporary copy and can be modified .)
(4)strtok The first argument to the function is not NULL , The function will find str First mark in ,strtok Function will hold its position in the string .
(5)strtok The first argument to the function is NULL , The function will start at the location where it is saved in the same string , Find next tag .
(6) If there are no more tags in the string , Then return NULL Pointer .
7.strerror
char *strerror( int errnum );
function :
strerror Function will errnum Map to error message string , And returns a pointer to the string
application :
#include<errno.h> int main() { FILE*pf=fopen("test.txt","r");//file open if (pf
== NULL) { printf("%s\n", strerror(errno)); } return 0; }
<> Character classification function

<> Memory operation function

1.memcpy
void *memcpy( void *dest, const void *src, size_t count );
function :

memcpy Function will src Of count Copy bytes to dest. If the source and target overlap , This function does not ensure that the original source bytes in the overlap area are copied before overriding . use memmove Dealing with overlapping areas .
application :
#include<stdio.h> int main() { int arr1[] = { 1, 2, 3, 4, 5, 6 }; int arr2[20]
= { 0 }; memcpy(arr2, arr1, 16); return 0; }
Simulation Implementation memcpy function
#include<stdio.h> #include<assert.h> void my_memecpy(void*dest, const void *src
, size_t count) { void *ret = dest; assert(dest&&src); while (count--) { *(char*
)dest = *(char*)src; ++(char*)dest; ++(char*)src; } return ret; } int main() {
int arr1[] = { 1, 2, 3, 4, 5, 6 }; int arr2[20] = { 0 }; my_memcpy(arr2, arr1,
16); return 0; }
2.memmove function
void *memmove( void *dest, const void *src, size_t count );
function :

memmove Function will count Byte characters from src Copy to dest. If some areas of the source area and the target area overlap ,memmove Will ensure that the original source bytes in the overlap area are copied before overriding .
application :
void test3() { int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; memeove(arr + 2,
arr, 16); } int main() { test3(); return 0; }
Simulation Implementation memmove
#include <stdio.h> #include <string.h> #include<assert.h> void* my_memmove(void
* dest, const void* src, size_t count) { assert(dest && src); void* start = dest
; if (dest < src) { // Copy from front to back while (count--) { *(char*)dest = *(char*)src; ++(
char*)dest; ++(char*)src; } } else { // Copy from back to front while (count--) { *((char*)dest +
count) = *((char*)src + count); } } return start; } int main() { int arr[] = {
1,2,3,4,5,6,7,8,9,10}; my_memmove(arr + 2,arr,16); }
3.memset function
memset Is the initialization function . The function is to set all the contents of a block of memory to the specified value , This function usually initializes the newly requested memory .
application :
int main() { int arr[10]={0}; memset(arr,1,40); return 0; }
4.memcmp function
function : It's the storage area str1 Storage area str2 Before n Two bytes to compare .
If the return value < 0, It means str1 less than str2.
If the return value > 0, It means str2 less than str1.
If the return value = 0, It means str1 be equal to str2.
application :
int main() { int arr1[] = { 1, 2, 3, 4, 5 }; int arr2[] = { 1, 2, 5, 6, 7 };
int ret=memcmp(arr1, arr2, 8); return 0; }

Technology
©2020 ioDraw All rights reserved
C# The name space of python To solve the problem of dictionary writing list in 【 skill 】 Simple solutions Mac Insufficient disk space use python turtle Drawing simple strokes C Language implementation of minesweeping game CLion Novice step on the pit :CMake project is not loaded I wish you all the best ( element ) Happy Lantern Festival ! Let's guess lantern riddles sewer , Dead rat , faeces ...... The smell is dregs in front of it 2020 Qizhi Developer Conference , These open classes are not to be missed !Mac disk space “ second ” Lose weight , Just because of this step of God operation !