一些库函数的实现

本文深入解析了C语言中常用的字符串操作函数,包括strlen、strcat、strcpy、strcmp、strchr、strstr、tolower、toupper、memmove、sqrt等,详细介绍了每个函数的功能、用法及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

size_t strlen( const char *string ) { const char *s; s = string; while ( *s ) { s++; } return s - string; } char *strcat( char *strDestination, const char *strSource ) { char *s; s = strDestination; while ( *s ) { s++; } while ( *strSource ) { *s++ = *strSource++; } *s = 0; return strDestination; } char *strcpy( char *strDestination, const char *strSource ) { char *s; s = strDestination; while ( *strSource ) { *s++ = *strSource++; } *s = 0; return strDestination; } int strcmp( const char *string1, const char *string2 ) { while ( *string1 == *string2 && *string1 && *string2 ) { string1++; string2++; } return *string1 - *string2; } char *strchr( const char *string, int c ) { while ( *string ) { if ( *string == c ) { return ( char * )string; } string++; } return (char *)0; } char *strstr( const char *string, const char *strCharSet ) { while ( *string ) { int i; for ( i = 0 ; strCharSet[i] ; i++ ) { if ( string[i] != strCharSet[i] ) { break; } } if ( !strCharSet[i] ) { return (char *)string; } string++; } return (char *)0; } #if !defined ( _MSC_VER ) && ! defined ( __linux__ ) && ! defined ( MAC_WOLF2_MP ) int tolower( int c ) { if ( c >= 'A' && c <= 'Z' ) { c += 'a' - 'A'; } return c; } int toupper( int c ) { if ( c >= 'a' && c <= 'z' ) { c += 'A' - 'a'; } return c; } #endif //#ifndef _MSC_VER void *memmove( void *dest, const void *src, size_t count ) { int i; if ( dest > src ) { for ( i = count-1 ; i >= 0 ; i-- ) { ((char *)dest)[i] = ((char *)src)[i]; } } else { for ( i = 0 ; i < count ; i++ ) { ((char *)dest)[i] = ((char *)src)[i]; } } return dest; } #if 0 double floor( double x ) { return (int)(x + 0x40000000) - 0x40000000; } void *memset( void *dest, int c, size_t count ) { while ( count-- ) { ((char *)dest)[count] = c; } return dest; } void *memcpy( void *dest, const void *src, size_t count ) { while ( count-- ) { ((char *)dest)[count] = ((char *)src)[count]; } return dest; } char *strncpy( char *strDest, const char *strSource, size_t count ) { char *s; s = strDest; while ( *strSource && count ) { *s++ = *strSource++; count--; } while ( count-- ) { *s++ = 0; } return strDest; } double sqrt( double x ) { float y; float delta; float maxError; if ( x <= 0 ) { return 0; } // initial guess y = x / 2; // refine maxError = x * 0.001; do { delta = ( y * y ) - x; y -= delta / ( 2 * y ); } while ( delta > maxError || delta < -maxError ); return y; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值