Function source code strcpy/memcpy/atoi/kmp/quicksort

Source: Internet
Author: User


I. Preface

After posting a blog about how students and sisters who participated in the autumn recruitment plan prepare for a job, I had a great deal of response. I want to sort it out here so that you can review it. Many blog posts originating from July are also compiled by myself. I hope everyone can write it again until it can be killed in seconds.


Ii. stl template Functions


1. strcpy

char * strcpy( char *strDest, const char *strSrc )        {           if(strDest == strSrc) { return strDest; }        assert( (strDest != NULL) && (strSrc != NULL) );           char *address = strDest;            while( (*strDest++ = * strSrc++) != '\0' );            return address;       }  

2. strncpy

char *strncpy(char *strDes, const char *strSrc, unsigned int count)        {            assert(strDes != NULL && strSrc != NULL);            char *address = strDes;            while (count-- && *strSrc != '\0')                *strDes++ = *strSrc++;         *strDes = '\0';        return address;        } 


3. strcmp

int strcmp(const char *s, const char *t)     {         assert(s != NULL && t != NULL);         while (*s && *t && *s == *t)         {             ++ s;             ++ t;         }         return (*s - *t);     } 

4. strcat

char *strcat(char *strDes, const char *strSrc)     {         assert((strDes != NULL) && (strSrc != NULL));         char *address = strDes;         while (*strDes != '\0')             ++ strDes;         while ((*strDes ++ = *strSrc ++) != '\0')             NULL;         return address;     } 

5. strlen

int strlen(const char *str)     {         assert(str != NULL);         int len = 0;         while (*str ++ != '\0')             ++ len;         return len;     }  

6. strstr

char *strstr(const char *strSrc, const char *str)     {         assert(strSrc != NULL && str != NULL);         const char *s = strSrc;         const char *t = str;         for (; *strSrc != '\0'; ++ strSrc)         {             for (s = strSrc, t = str; *t != '\0' && *s == *t; ++s, ++t)                 NULL;             if (*t == '\0')                 return (char *) strSrc;         }         return NULL;     }   

7. strncat

char *strncat(char *strDes, const char *strSrc, unsigned int count)     {         assert((strDes != NULL) && (strSrc != NULL));         char *address = strDes;         while (*strDes != '\0')             ++ strDes;         while (count -- && *strSrc != '\0' )             *strDes ++ = *strSrc ++;         *strDes = '\0';         return address;     }

8. strncmp

int strncmp(const char *s, const char *t, unsigned int count)     {         assert((s != NULL) && (t != NULL));         while (*s && *t && *s == *t && count --)         {             ++ s;             ++ t;         }         return (*s - *t);     }

9. memcpy

void *memcpy(void *dest, const void *src, unsigned int count)     {         assert((dest != NULL) && (src != NULL));         void *address = dest;         while (count --)         {             *(char *) dest = *(char *) src;             dest = (char *) dest + 1;             src = (char *) src + 1;         }         return address;     } 

10. memccpy

void *memccpy(void *dest, const void *src, int c, unsigned int count)     {         assert((dest != NULL) && (src != NULL));         while (count --)         {             *(char *) dest = *(char *) src;             if (* (char *) src == (char) c)                 return ((char *)dest + 1);             dest = (char *) dest + 1;             src = (char *) src + 1;         }         return NULL;     } 

11. memcmp

int memcmp(const void *s, const void *t, unsigned int count)     {         assert((s != NULL) && (t != NULL));         while (*(char *) s && *(char *) t && *(char *) s == *(char *) t && count --)         {             s = (char *) s + 1;             t = (char *) t + 1;         }         return (*(char *) s - *(char *) t);     }

12. memmove

/// @ Big: // handle the overlap between src and dest. It's okay not to start moving from the tail. // One case is that the dest is smaller than src and there is an overlap. In this case, the dest must start from the beginning. // The other case is that there is an overlap between the dest and src. In this case, the dest must start from the end. Void * memmove (void * dest, const void * src, unsigned int count) {assert (dest! = NULL & src! = NULL); char * pdest = (char *) dest; char * psrc = (char *) src; // when pdest is behind psrc and the distance between the two is smaller than count, move from the end. otherwise, move the if (pdest> psrc & pdest-psrc <count) {while (count --) {* (pdest + count) from the header) = * (psrc + count) ;}} else {while (count --) {* pdest ++ = * psrc ++ ;}} return dest ;}

13. memset

void *memset(void *str, int c, unsigned int count)     {         assert(str != NULL);         void *s = str;         while (count --)         {             *(char *) s = (char) c;             s = (char *) s + 1;         }         return str;     }


Iii. atoi & itoa


1. atoi


2. itoa



Iv. kmp & quicksort


1. kmp


2. quicksort









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.