C语言字符串库函数的实现

[来源] 达内    [编辑] 达内   [时间]2012-09-08

C语言字符串库函数的实现也是笔试题常考的题目,以下代码没有严格测试,只是简单的实现

C语言字符串库函数的实现也是笔试题常考的题目,以下代码没有严格测试,只是简单的实现:

< div style="margin: 5px 0px; padding: 5px; background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
//
字符串长度

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;        }  
//
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; }  //
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;    }     //
字符串比较

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

 (*s && *t && *s == *t)        {            
++ s;            ++
 t;        }        

return (*s - *t);    }     
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);    }  
//字符串连接

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;      }
< p style="margin: 5px auto; padding: 0px; text-indent: 0px; line-height: 19px; color: rgb(0, 0, 0); font-size: 13px; font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: left; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 242); ">c标准库部分源代码

< div style="margin: 5px 0px; padding: 5px; background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
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);   } 

资源下载