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