Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C language to realize common string library functions

Shulou Source: shulou.com Published: 2022-06-02 17:51:20 10月03日 Update

This article introduces the knowledge of "how to use C language to implement common string library functions". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, realize memcpy

Copy what src points to to the location where dst points, and copy len bytes.

Memcpy is a memory copy function

Memcpy is copied in bytes regardless of type when using it.

It doesn't stop when you encounter "\ 0", so it's better to use strcpy when copying strings, which is more secure.

Void * Mymemcpy (void * dst, const void * src, int len) {void * ret = dst; assert (src); assert (dst); while (len--) {* (char *) dst = * (char *) src; dst = (char *) dst + 1; src = (char *) src + 1;} return ret } int main () {char str [100] = "ABCDE"; Mymemcpy (str, str + 2,2); printf ("% s", str); system ("pause"); return 0;}

But when we modified the test case, we found the following results

The reason for this is very simple, because the first address of our source string is lower than the first address of the target string, and when we copy from left to right by default, the first string will overwrite the second string, so there is a full A situation. This is the memory overlap copy. To solve this problem, it is very simple to add a judgment before copying. If the first address of the source string is lower than the first address of the target string, we copy it from right to left, which solves the problem perfectly.

Second, memmove simulation implementation

Here we repeat the previous test case and find that there is no full An after adding judgment and copying it from right to left.

Void * Mymemmove (void * dst, const void * src, int len) {char* _ src = (char*) src; char* _ dst = (char*) dst; if (_ dst > _ src&&_dst)

< _src + len){ _dst = _dst + len - 1; _src = _src + len - 1; while (len--){ *_dst = *_src; _dst--; _src--; } } else { while (len--){ *_dst = *_src; _dst++; _src++; } } return dst;}int main(){ char str[100] = "ABCDE"; int len = strlen(str); memcpy(str+1, str , len); printf("%s", str); system("pause"); return 0;} 三、strlen的模拟实现1.计数器方法:int Mystrlen(char * str){ int count = 0; while (*str != 0){ str++; count++; } return count;}int main(){ char a[] = "dadai"; int aa=Mystrlen(a); printf("%d", aa); system("pause"); return 0;}2.递归方式:int Mystrlen(char * str){ if (*str == '\0'){ return 0; } else return (1 + Mystrlen(str + 1));}//1+1+1+1+1+0int main(){ char a[] = "dadai"; int aa=Mystrlen(a); printf("%d", aa); system("pause"); return 0;}3.利用指针实现:int Mystrlen(char * str){ char *p = str; while (*p){ p++; } return (p - str);}int main(){ char a[] = "dadai"; int aa=Mystrlen(a); printf("%d", aa); system("pause"); return 0;}四、strcpy的模拟实现 将src所指向的内容拷贝到dst所指向的存储单元。 char * Mycpy(char *dst, const char * src){assert(dst != NULL && src !=NULL);char *Mycpy = dst;while((*dst++ = *src++) != '\0');return Mycpy;}int main(){ char src []= "daadok"; char dst[10] ; Mycpy(dst, src); printf("%s", dst); system("pause"); return 0;}五、strcmp的模拟实现 strcmp用于比较两个字符串是否相等,若相等则返回0,若dst>

Src returns 1, otherwise it returns-1.

Int Mystrcmp (const char* dst,const char* src) {while (* dst&&*src&& (* dst = = * src)) {dst++; src++;} if (* dst > * src) {return 1;} else if (* dst)

< *src){ return -1; } else if (*dst == *src){ return 0; }}int main(){ char str1[] = "abcdefg"; char str2[] = "dfd"; int a=Mystrcmp(str1, str2);; printf("%d", a); system("pause"); return 0;}六、strstr模拟实现 在dst中找到第一次与src相等的位置并输出这个位置之后dst所指向的内容。 char *mystrstr( const char *dst,const char *src){ assert(dst); assert(src); const char *p = dst; while (*p){ const char *movep = p; const char *sp = src; while(*movep && *sp&& *sp== *movep){ sp++; movep++; } if (*sp == '\0'){ return (char *)p; } p++; } return NULL;}int main(){ const char* dst = "abcd123456"; const char*src = "cd"; char *ret= mystrstr(dst, src); printf("%s", ret); system("pause"); return 0;} 七、模拟实现strcat 把src所指向的内容拼接到dst所指向内容的末尾。 char * mystrcat(char*dst, const char*src){ assert(src); assert(dst); char*ret = dst; while (*dst){ dst++; } while (*src){ *dst = *src; dst++; src++; } return ret;}int main(){ char dst[64] = "abcd"; char *src = "efg"; mystrcat(dst, src); printf("%s", dst); system("pause"); return 0;}

This is the end of the content of "how to use C language to implement common string library functions". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Copy character string content point address situation time function location commonly used language memory bytes more goal knowledge problem test output Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MySQL Linux vpn Docker