1. 程式人生 > >C/C++實現strcpy,strcat,strlen,strcmp標準實現程式碼

C/C++實現strcpy,strcat,strlen,strcmp標準實現程式碼

網上很容易找的到,拿出來記錄下來備忘:)

char * strcpy (char * dst, char * src)
{
      char * cp = dst;

      while( *cp++ = *src++ )
      ;               /* Copy src over dst */
      return( dst );
}

char * strcat (char * dst, char * src)
{
      char * cp = dst;

      while( *cp )
           ++cp;           /* Find end of dst */
      while( *cp++ = *src++ )
      ;               /* Copy src to end of dst */
      return( dst );

}

int strlen (const char * str)
{
     int length = 0;

     while( *str++ )
          ++length;

     return( length );
}

int strcmp (unsigned char *src, unsigned char *dst)
{
        int ret = 0 ;

        while( ! (ret = *src - *dst) && *dst)
             ++src, ++dst;

        if ( ret < 0 )
              ret = -1 ;
        else if ( ret > 0 )
              ret = 1 ;

        return( ret );
}