STR Series Library functions--simulation implementation

Source: Internet
Author: User

Strlen
#include <string.h>
size_t strlen (char *str);

Function: The function returns the length of the string str (that is, the number of characters before the null terminator).

Non-recursive implementation

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

Recursive implementation

int my_strlen2 (const char* strdest)//recursive string length {assert (strdest), if (' = = = *strdest ') return 0;elsereturn (1 + my_strlen (+ +) strdest));}


strcpy
#include <string.h>
Char *strcpy (char *to, const char *from);


Function: Copies the characters from the from character to the string to, including the null-value terminator. The return value is pointer to.

Do not consider memory overlap

char* my_strcpy (char* DST, const char* SRC) {assert (DST && src); char* pdst = Dst;while ((*pdst++ = *src++)! = ' + ') ; 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))//overlap {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 <string.h>
Char *strncpy (char *to, const char *from, size_t count);


Function: Copies the string from a maximum of count characters to the string to. If the length of the string from is less than count, the remainder is filled with '% '. Returns a string that handles completion.

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 + +)! = ' + ') {--n;} if (n)//If N==0 then the following will die loop {while (n--) {*pdst++ = '} ';}} return DST;}


Strstr
#include <string.h>
Char *strstr (const char *STR1, const char *STR2);


Function: The function returns a pointer to the position of the string str2 first appearing in the string str1, and returns null if it is not found.

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 be terminated in advance loop {srctmp = (char*) src;subtmp = (char*) Sub while (*srctmp = = *subtmp) {if (*subtmp = = ') return (char*) src;++srctmp;++subtmp;} if (*subtmp = = ') return (char*) src;++src;} return NULL;}


Strcat
#include <string.h>
Char *strcat (char *str1, const char *STR2);


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

char* My_strcat (char* DST, const char* str) {assert (DST && str); char* pdst = Dst;while (*pdst! = ')//Note This cannot be a * pdst++! = ' + ' This will add a!++pdst;while (*str! = ') ' *pdst++ = *STR++;*PDST = ' + '; return DST;}


Strncat
#include <string.h>
Char *strncat (char *str1, const char *STR2, size_t count);

Function: Appends the string from a maximum count of characters to the string to, appending a null terminator to the connect prompt. Returns a string that handles completion.

char* My_strncat (char *dst, const char *STR, size_t N) {assert (DST && str); char* pdst = Dst;while (*pdst! = ') + +  Pdst;while (n && (*pdst = *str)! = ' + ') {--n;++pdst; It is better to do so, do not put in while inside ++str;} *PDST = ' + '; return DST;}


strcmp
#include <string.h>
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 <string.h>
int strncmp (const char *STR1, const char *STR2, size_t count);

Function: Compares strings str1 and str2 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 any of the string lengths in the argument is less than count, the processing is ended when the first null terminator is compared.

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)) {++pstr 1;++PSTR2;} return *PSTR1-*pstr2;}



650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0041.gif "alt=" J_0041.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0041.gif "style=" white-space:normal; "alt=" J_0041.gif "/>650" this.width=650; "src = "Http://img.baidu.com/hi/jx2/j_0041.gif" style= "white-space:normal;" alt= "J_0041.gif"/>650 "this.width=650;" Src= "Http://img.baidu.com/hi/jx2/j_0041.gif" style= "White-space:normal; alt=" J_0041.gif "/>650) this.width=650 , "src=" Http://img.baidu.com/hi/jx2/j_0041.gif "style=" White-space:normal; alt= "J_0041.gif"/>650) this.width= 650, "src=" Http://img.baidu.com/hi/jx2/j_0041.gif "style=" white-space:normal; "alt=" J_0041.gif "/>650" this.width=650, "src=" Http://img.baidu.com/hi/jx2/j_0041.gif "style=" White-space:normal; alt= "J_0041.gif"/> 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0041.gif "style=" White-space:normal "alt=" j_0041.gif "/ >650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0041.gif "alt=" J_0041.gif "/>650) this.width=650, "src=" Http://img.baidu.com/hi/jx2/j_0041.gif "style=" White-space:normal; alt= "J_0041.gif"/> 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0041.gif "style=" White-space:normal "alt=" j_0041.gif "/ >650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0041.gif "style=" White-space:normal "alt=" j_0041.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0041.gif "style=" White-space:normal "alt=" j_0041. GIF "/>

This article is from the "11408774" blog, please be sure to keep this source http://11418774.blog.51cto.com/11408774/1847047

STR Series Library functions--simulation implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.