C语言字符串库函数的实现
C语言字符串库函数的实现也是笔试题常考的题目,以下代码没有严格测试,只是简单的实现:
//字符串长度 int strlen(const char *str) { assert(str != NULL); int len = 0; while (*str ++ != '\0') ++ len; return len; } //字符串拷贝 char *strcpy(char *to, const char *from) { assert((to != NULL) && (from != NULL)); char * result = to; while( (*to++ = *from++) != '\0') NULL; return result; } //strncpy(),如果from指向的字符个数少于count,则用'\0'补齐 char *strncpy(char *to, const char *from, size_t count) { assert((to != NULL) && (from != NULL)); char * result = to; while(count--) { if(*from != '\0') { *to++ = *from++; } else { *to++ = '\0'; } } return result; } //memset():把指定内存区域的前count个字节设置成字符c void * memset(void* buffer, int c, size_t count) { assert(buffer != NULL); char * p = (char *)buffer; while(count--) *p++ = (char)c; return buffer; } //查找字符串s中首次出现字符c的位置 char *strchr(char *str, int c) { assert(str != NULL); for (; *str != (char)c; ++ str) if (*str == '\0') return NULL; return str; } //字符串连接 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; } 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; } //查找字符串第一次出现的位置 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; } //将字符串拷贝到新的位置 char *strdup_(char *strSrc) { if(strSrc!=NULL) { char *start=strSrc; int len=0; while(*strSrc++!='\0') len++; char *address=(char *)malloc(len+1); assert(address != NULL); while((*address++=*start++)!='\0'); return address-(len+1); } return NULL; }
内存拷贝函数:
//memcpy(), 拷贝不重叠的内存块 void* memcpy(void* to, const void* from, size_t count) { assert((to != NULL) && (from != NULL)); void * result = to; char * pto = (char *)to; char * pfrom = (char *)from; assert(pto < pfrom || pto > pfrom + count -1); while(count--) { *pto++ = *pfrom++; } return result; } //memmove(), 拷贝重叠或者是不重叠的内存块 void* memmove(void* to, const void* from, size_t count) { assert((to != NULL) && (from != NULL)); void * result = to; char * pto = (char *)to; char * pfrom = (char *)from; //to与from没有重叠 if(pto < pfrom || pto > pfrom + count -1) { while(count--) { *pto++ = *pfrom++; } } //to与from有重叠,从后向前move else { pto = pto + count -1; pfrom = pfrom + count -1; while(count--) { *pto-- = *pfrom--; } } return result; }
字符串比较函数:
//字符串比较 int strcmp(const char *s, const char *t) { assert(s != NULL && t != NULL); while(*s && *t && *s == *t) { ++ s; ++ t; } return (*s - *t); } //字符串比较(不区分大小写比较,大写字母会被映射为小写字母) int stricmp(const char *dst, const char *src) { assert(s != NULL && t != NULL); int ch1, ch2; while(*dst && *src) { if((ch1 = (int)*dst) >= 'A' && (ch1 <= 'Z')) ch1 += 0x20; if((ch2 = (int)*src) >= 'A' && (ch2 <= 'Z')) ch2 += 0x20; if(ch1 == ch2) { ++ dst; ++ src; } else break; } return(ch1 - ch2); } 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); }
c标准库部分源代码
char * __cdecl strcat (char * dst,const char * src) { char * cp = dst; while( *cp ) cp++; /* find end of dst */ while( *cp++ = *src++ ) ; /* Copy src to end of dst */ return( dst ); /* return dst */ } int __cdecl strcmp (const char * src,const char * dst) { int ret = 0 ; while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst) ++src, ++dst; if ( ret < 0 ) ret = -1 ; else if ( ret > 0 ) ret = 1 ; return( ret ); } size_t __cdecl strlen (const char * str) { const char *eos = str; while( *eos++ ) ; return( (int)(eos - str - 1) ); } char * __cdecl strncat (char * front,const char * back,size_t count) { char *start = front; while (*front++) ; front--; while (count--) if (!(*front++ = *back++)) return(start); *front = '\0'; return(start); } int __cdecl strncmp (const char * first,const char * last,size_t count) { if (!count) return(0); while (--count && *first && *first == *last) { first++; last++; } return( *(unsigned char *)first - *(unsigned char *)last ); } /* Copy SRC to DEST. */ char * strcpy (char * dest,const char* src) { reg_char c; char *__unbounded s = (char *__unbounded) CHECK_BOUNDS_LOW (src); const ptrdiff_t off = CHECK_BOUNDS_LOW (dest) - s - 1; size_t n; do { c = *s++; s[off] = c; } while (c != '\0'); n = s - src; (void) CHECK_BOUNDS_HIGH (src + n); (void) CHECK_BOUNDS_HIGH (dest + n); return dest; } char * __cdecl strncpy (char * dest,const char * source,size_t count) { char *start = dest; while (count && (*dest++ = *source++)) /* copy string */ count--; if (count) /* pad out with zeroes */ while (--count) *dest++ = '\0'; return(start); }