Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to simulate and realize str Series Library functions

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to simulate the implementation of str series library functions", the content is simple and easy to understand, clear organization, I hope to help you solve doubts, let Xiaobian lead you to study and learn "how to simulate the implementation of str series library functions" this article bar.

strlen #include size_t strlen( char *str );

Function: Returns the length of string str (i.e. the number of characters before the null terminator).

nonrecursive realization

size_t my_strlen(const char* src){ assert(src); size_t len = 0; while (*src++ != '\0') ++len; return len;}

recursive implementation

int my_strlen2(const char* strDest) //recursively find string length { assert(strDest); if ('\0' == *strDest) return 0; else return (1 + my_strlen(++strDest));}strcpy #include char *strcpy( char *to, const char *from );

Function: Copy characters from string from to string to, including null terminator. Return value is pointer to.

Regardless of memory overlap

char* my_strcpy(char* dst, const char* src){ assert(dst && src); char* pdst = dst; while ((*pdst++ = *src++) != '\0'); return dst;}

Consider memory overlap

char *my_strcpy(char *dst,const char *src){ assert(dst && src); char *ret = dst; my_memcpy(dst,src,strlen(src)+1); return ret;}

where my_memcpy is implemented as follows:

void * my_memcpy(void *dst, const void *src, size_t count){ assert(dst && src); char* pdst = (char*)dst; char* psrc = (char*)src; int n = count; if (pdst > psrc && pdst

< (psrc + count)) //重叠 { for (size_t i = n - 1; i != -1; --i) pdst[i] = psrc[i]; } else { for (size_t i = 0; i < n; ++i) pdst[i] = psrc[i]; } return dst;} strncpy #include char *strncpy( char *to, const char *from, size_t count ); 功能:将字符串from 中至多count个字符复制到字符串to中。如果字符串from 的长度小于count,其余部分用'\0'填补。返回处理完成的字符串。 char* my_strncpy(char* dst, const char* src, size_t n) { assert(dst && str); char* pdst = dst; const char* psrc = src; while (n && (*pdst++ = *psrc ++) != '\0') { --n; } if (n) //如果n==0 则下面会死循环 { while (n--) { *pdst++ = '\0'; } } return dst;} strstr #include char *strstr( const char *str1, const char *str2 ); 功能:函数返回一个指针,它指向字符串str2 首次出现于字符串str1中的位置,如果没有找到,返回NULL。 char* my_strstr(const char* src, const char* sub) { assert(src && sub); int sublen = strlen(sub); char* srctmp = NULL; char* subtmp = NULL; while (strlen(src) >

= sublen) //strlen(src) is constantly updated and can end the loop early { srctmp = (char*)src; subtmp = (char*)sub; while (*srctmp == *subtmp) { if (*subtmp == '\0') return (char*)src; ++srctmp; ++subtmp; } if (*subtmp == '\0') return (char*)src; ++src; } return NULL;} strcat #include char *strcat( char *str1, const char *str2 );

Function: The function concatenates the string str2 to the end of str1 and returns the pointer str1.

char* my_strcat(char* dst, const char* str){ assert(dst && str); char* pdst = dst; while (*pdst != '\0')//Note that this cannot be *pdst++ != '\0' will add one more time! ++pdst; while (*str != '\0') *pdst++ = *str++; *pdst = '\0'; return dst;} strncat #include char *strncat( char *str1, const char *str2, size_t count );

Function: Concatenate at most count characters in the string from into the string to, appending a null terminator. Returns the processed string.

char* my_strncat(char *dst, const char *str, size_t n){ assert(dst && str); char* pdst = dst; while (*pdst != '\0') ++pdst; while (n && (*pdst = *str) != '\0') { --n; ++pdst; //better to do this, don't put it inside while ++str; } *pdst = '\0'; return dst;} strcmp #include int strcmp( const char *str1, const char *str2 );

Function: Compare string str1 and str2, the return value is as follows:

return value

explain

less than 0

str1 is less than str2

equal to 0

str1 is equal to str2

greater than 0

str1 is greater than str2

int my_strcmp(const char *str1, const char *str2){ assert(str1 && str2); const char* pstr1 = str1; const char* pstr2 = str2; while (*pstr1 && *pstr2 && (*pstr1 == *pstr2)) { ++pstr1; ++pstr2; } return *pstr1 - *pstr2;} strncmp #include int strncmp( const char *str1, const char *str2, size_t count );

Function: Compare strings str1 and str2 for up to count characters. The return values are as follows:

return value

explain

less than 0

str1 is less than str2

equal to 0

str1 is equal to str2

greater than 0

str1 is greater than str2

If the length of any string in the argument is less than count, the comparison ends when the first null terminator is reached.

int my_strncmp(const char *str1, const char *str2, size_t n){ assert(str1 && str2 && n > 0); const char* pstr1 = str1; const char* pstr2 = str2; while (--n && *pstr1 && *pstr2 && (*pstr1 == *pstr2)) { ++pstr1; ++pstr2; } return *pstr1 - *pstr2;} This is all the content of "How to simulate str series library functions", thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report