The implementation of the "C language" Str class and the men library function (e.g., strcpy,strcmp,strstr,strcat,memmove,memcpy)

Source: Internet
Author: User
Tags assert strcmp

    1. strcpy

Copies the source string to a substring, including '/'.

Code implementation:

char* strcpy (char* dst,const char* src) {assert (SRC);    char* ret = DST;        while (*SRC) {*dst = *SRC;        src++;    dst++;    } *DST = ' + '; return ret;}



2.strncpy:

The difference between strncpy and strcpy is that strcpy copies all the source strings into a new string, and the strncpy copy length is determined by itself.

Code implementation:

char* strncpy (char* DST, const char* SRC, int count) {assert (DST);    ASSERT (SRC);    char* ret = DST;        while (count--) {*dst = *SRC;        dst++;    src++;    } *DST = ' + '; return ret;}


3.strcat:

The strcat function is a link string, i.e.:

Str1:hel Str2:lo is linked to Hello.

Code implementation:

char* strcat (char* DST, char* src) {assert (DST);    ASSERT (SRC);    char* ret = src;    while (*SRC) {src++;        } while (*DST) {*src = *DST;        dst++;    src++;    } *DST = ' + '; return ret;}


4.strcmp:

Strcmp is used to compare string lengths.

The two strings are compared from left to right by character (by ASCII value size) until different characters are present or when ' + ' is encountered. If all characters are the same, they are considered equal; If a different character is present, the result of the comparison of the first different character will prevail.
If the two strings are composed of English letters, there is a simple rule: in the English dictionary position in the back of the "big", but also pay special attention to: lowercase letters than uppercase "big".
return value:
(1) String 1 = string 2, return 0
(2) string 1> String 2, returns a positive integer
(3) string 1< String 2, returns a negative integer.

Code implementation:

int strcmp (const char* dst, const char* src ) {    assert (DST);     assert (SRC);    while  ( *SRC&&*DST)     {        if  (*src &NBSP;==&NBSP;*DST)         {             src++;             Dst++;        }        else         {             return *src - *dst -  ';        '  }    }    return *src - *dst -  ' + ';} 


5.strncmp:

The difference with strcmp is that strcmp is for the entire string, and strncmp is for the specified length.

Note, however, that if count is shorter than the length of both strings, you want to jump out of the loop. When the length is greater than the length of both strings, the equality can still be compared.

Code implementation:

int strncmp (const char* DST, const char* src,size_t count) {assert (DST);    ASSERT (SRC);            while (COUNT--&AMP;&AMP;*SRC&AMP;&AMP;*DST) {if (*src = = *dst) {src++;        dst++;        } else {return *src-*dst-' + '; }} return *src-*dst-' + ';}


6.strstr:

Looking for a substring, we set a pointer in the source string to be used when it is true that the substring is at the location of the original string, as in the following p. And the s1,s2 are used to traverse them respectively.

Code implementation:

Char* strstr (CONST&NBSP;CHAR*&NBSP;DST,&NBSP;CONST&NBSP;CHAR*&NBSP;SRC) {    assert ( DST);     assert (SRC);    char* s1 = dst;     char* p = src;    char* s2 = p;     while  (*S2)     {        s1 =  dst;        s2 = p;         while  (*S2&NBSP;&AMP;&AMP;&NBSP;*S1)         {             if  (*S2&NBSP;==&NBSP;*S1)              {                 s1++;                 s2++;            }             else             {                 p++;                  break;            }         }        if  (*s1 ==   ' + ')         {             return p;        }     }    return null;}


7.memcpy:

strcpy completes a copy of the string, and for a non-string class, it uses memcpy to complete the memory copy.

Code implementation:

void* memcpy (void* DST, const void* SRC, size_t count) {assert (DST);    ASSERT (SRC);    char* dst_ = (char*) DST;    char* src_ = (char*) src;    while (count--) {*dst_++ = *src_++;    }//Even if Count is not 0 at this time, but when we copy the original number to the end of the new data, it also ends the program. *dst_ = ' + ';//must be added to the end flag, otherwise it will be garbled return DST;}


8.Memmove:

The memmove is that it solves the memory overlap problem.

For example, move the 1,2,3,4 in 1,2,3,4,5,6,7,8 to the 3,4,5,6 position. Then still follow memcpy will, move 1 to 3, 2 move to 4, and then prepare to move 3 o'clock found at this time the 3 has been moved to the 1 coverage here and lost. 4 similarly. This is the advantage of memmove. We can work it out in a few cases.

Code implementation:

Void memmove (Void* dst, const void* src, size_t count) {     assert (DST);     assert (SRC);    char* dst_ =  ( char*) dst;    char* src_ =  (char*) src;    if  (dst_  > src_&&dst < dst_ + count)     {         while  (count--)         {             * (Dst_+count)  = * (src_+count );            dst_++;             src_++;        }     }    else    {         while  (Count--)         {             *dst_ = *src_;             dst_++;            src_++;         }    }    *dst_ =  ';  ' &NBSP;&NBSP;&NBSP;RETURN&NBSP;DST;}


This article is from the "C language 100-200 Prime" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1765040

The implementation of the "C language" Str class and the men library function (e.g., strcpy,strcmp,strstr,strcat,memmove,memcpy)

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.