1、以下是一些常见的 C 字符串处理函数的源代码,当然也是从网上找来的,学习它一个好处是加深理解 C,另一个好外是应聘面试中的笔试常常会用到它们,呵呵。当然,还有一些没有列出来,或者有更好的实现方法,你可以贡献一下哦,share your code! :)/stirng.c=char *strcpy(char *strDes, const char *strSrc)assert(strDes != NULL) /如果条件为假,就打印具体出错的问题char *address = strDes;while (*strDes + = *strSrc +) != 0)NULL;return address
2、;char *strchr_(char *str, int c)assert(str != NULL);while (*str != (char) c) if (*str != 0)return str;return NULL;char *strchr(const char *str, int c)assert(str != NULL);for (; *str != (char) c; + str)if (*str = 0)return NULL;return (char *) str;int strcmp(const char *s, const char *t)assert(s != NU
3、LL while (*s + t;return (*s - *t);char *strcat(char *strDes, const char *strSrc)assert(strDes != NULL) char *address = strDes;while (*strDes != 0)+ strDes;while (*strDes + = *strSrc +) != 0)NULL;return address;int strlen(const char *str)assert(str != NULL);int len = 0;while (*str + != 0)+ len;return
4、 len;char *strdup(const char *strSrc)assert(strSrc != NULL);int len = 0;while (*strSrc + != 0)+ len;char *strDes = (char *) malloc (len + 1);while (*strDes + = *strSrc +) != 0)NULL;return strDes;char *strstr(const char *strSrc, const char *str)assert(strSrc != NULL const char *s = strSrc;const char
5、*t = str;for (; *t != 0; + strSrc)for (s = strSrc, t = str; *t != 0 +s, +t)NULL;if (*t = 0)return (char *) strSrc;return NULL;char *strncpy(char *strDes, const char *strSrc, int count)assert(strDes != NULL char *address = strDes;while (count - return address;char *strncat(char *strDes, const char *s
6、trSrc, int count)assert(strDes != NULL) char *address = strDes;while (*strDes != 0)+ strDes;while (count - *strDes = 0;return address;int strncmp(const char *s, const char *t, int count)assert(s != NULL) while (*s + t;return (*s - *t);char *strpbrk(const char *strSrc, const char *str)assert(strSrc !
7、= NULL) const char *s;while (*strSrc != 0)s = str;while (*s != 0)if (*strSrc = *s)return (char *) strSrc;+ s;+ strSrc;return NULL;int strcspn(const char *strSrc, const char *str)assert(strSrc != NULL) const char *s;const char *t = strSrc;while (*t != 0)s = str;while (*s != 0)if (*t = *s)return t - s
8、trSrc;+ s;+ t;return 0;int strspn(const char *strSrc, const char *str)assert(strSrc != NULL) const char *s;const char *t = strSrc;while (*t != 0)s = str;while (*s != 0)if (*t = *s)break;+ s;if (*s = 0)return t - strSrc;+ t;return 0;char *strrchr(const char *str, int c)assert(str != NULL);const char
9、*s = str;while (*s != 0)+ s;for (- s; *s != (char) c; - s)if (s = str)return NULL;return (char *) s;char* strrev(char *str)assert(str != NULL);char *s = str, *t = str, c;while (*t != 0)+ t;for (- t; s = a s +;return str;void *memcpy(void *dest, const void *src, int count)assert(dest != NULL) void *a
10、ddress = dest;while (count -)*(char *) dest = *(char *) src;dest = (char *) dest + 1;src = (char *) src + 1;return address;void *memccpy(void *dest, const void *src, int c, unsigned int count)assert(dest != NULL) while (count -)*(char *) dest = *(char *) src;if (* (char *) src = (char) c)return (cha
11、r *)dest + 1);dest = (char *) dest + 1;src = (char *) src + 1;return NULL;void *memchr(const void *buf, int c, int count)assert(buf != NULL);while (count -)if (*(char *) buf = c)return (void *) buf;buf = (char *) buf + 1;return NULL;int memcmp(const void *s, const void *t, int count)assert(s != NULL
12、) while (*(char *) s t = (char *) t + 1;return (*(char *) s - *(char *) t);void *memmove(void *dest, const void *src, int count)assert(dest != NULL void *address = dest;while (count -)*(char *) dest = *(char *) src;dest = (char *) dest + 1;src = (const char *)src + 1;return address;void *memset(void *str, int c, int count)assert(str != NULL);void *s = str;while (count -)*(char *) s = (char) c;s = (char *) s + 1;return str;