实现字符串库函数

1.strcpy

//char *strcpy( char *strDestination, const char *strSource );

#include <string.h>
#include <stdio.h>

void main( void )
{
   char string[80];
   strcpy( string, "Hello world from " );
   printf( "String = %s\n", string );
}

1.1mystrcpy

char *mystrcpy( char *strDestination, const char *strSource )
{
    
    if (strDestination == NULL || strSource == NULL) {
        return NULL; // 如果任意一个指针为 NULL,返回 NULL 表示错误
    }

    while(strDestination++ = strSource++);
    
    return strDestination;
}

2. strlen

//size_t strlen( const char *string );
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>

void main( void )
{
   char buffer[61] = "How long am I?";
   int  len;
   len = strlen( buffer );
   printf( "'%s' is %d characters long\n", buffer, len );
}

2.1mystrlen

size_t mystrlen( const char *string )
{
    if(string == NULL)
        return 0;
    
    char * s = string;
    while(*string != '\0')
    {
         string++;
     }
    return string - s;

}

3.strcat

char *strcat( char *strDestination, const char *strSource );


#include <string.h>
#include <stdio.h>

void main( void )
{
   char string[80];
   strcpy( string, "Hello world from " );
   strcat( string, "strcpy " );
   strcat( string, "and " );
   strcat( string, "strcat!" );
   printf( "String = %s\n", string );
}


Output

String = Hello world from strcpy and strcat

3.1mystrcat

char *mystrcat( char *strDestination, const char *strSource )
{
    if(strDestination == NULL || strSource == NULL)
    {
        return NULL;
    }
    
    char* str = strDestination;
    
    while(*str++);
    str--;
    while(*str++ = *strSource++);

    return strDestination;

}


4.atoi

//int atoi( const char *string );

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
   char *s; double x; int i; long l;

   s = "  -2309.12E-15";    /* Test of atof */
   x = atof( s );
   printf( "atof test: ASCII string: %s\tfloat:  %e\n", s, x );

   s = "7.8912654773d210";  /* Test of atof */
   x = atof( s );
   printf( "atof test: ASCII string: %s\tfloat:  %e\n", s, x );

   s = "  -9885 pigs";      /* Test of atoi */
   i = atoi( s );
   printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

   s = "98854 dollars";     /* Test of atol */
   l = atol( s );
   printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}


Output

atof test: ASCII string:   -2309.12E-15   float:  -2.309120e-012
atof test: ASCII string: 7.8912654773d210   float:  7.891265e+210
atoi test: ASCII string:   -9885 pigs      integer: -9885
atol test: ASCII string: 98854 dollars      long: 98854

4.1 myatoi

int myatoi( const char *string )
{
    if(string == NULL)
    {
        return -1;       
    }
    int ret = 0;
    int sign = 1;
    
    while(*string == ' ')
    {
        string++;
    }

    if(*string == '-')
    {
        string++;
        sign == -1;
    }
    else if(*string == '+')
    {
        string++;
    }
    
    while(*string != '\0' && isdigit(*string))
    {
        ret = ret*10 +(*string - '0');
        string++;
    }    

    return sign*ret;
    
    

    
}

5.itoa

//char *_itoa( int value, char *string, int radix );
#include <stdlib.h>
#include <stdio.h>

void main( void )
{
   char buffer[20];
   int  i = 3445;
   long l = -344115L;
   unsigned long ul = 1234567890UL;

   _itoa( i, buffer, 10 );
   printf( "String of integer %d (radix 10): %s\n", i, buffer );
   _itoa( i, buffer, 16 );
   printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
   _itoa( i, buffer, 2  );
   printf( "String of integer %d (radix 2): %s\n", i, buffer );

   _ltoa( l, buffer, 16 );
   printf( "String of long int %ld (radix 16): 0x%s\n", l, 
                                                    buffer );

   _ultoa( ul, buffer, 16 );
   printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,
                                                    buffer );
}


Output

String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): 0xd75
String of integer 3445 (radix 2): 110101110101
String of long int -344115 (radix 16): 0xfffabfcd
String of unsigned long 1234567890 (radix 16): 0x499602d2

5.1myitoa

char * myitoa( int value, char *string, int radix )
{
    char* str = string;
    int sign = value, i = 0, j = 0;
    if(sign < 0)
    {
        value = -value;
        sign = -1;
    }
    else
    {
        sign = 1;
    }

    while(value)
    {
        if(value < radix)
        {
            *str++ = value + '0';
            break;
        }
        *str++ = value % radix  + '0';
        value = value / radix;    
    }

    if(sign == -1)
    {
        *str++ = '-';
    }
    *str = '\0';

    j = str - string -1;
    
    for(i = 0; i < j; i++)
    {
        int tmp = str[i];
        str[i] = str[j];
        str[j--] = tmp;
    }
    
    return str;

}

6.memmove

void *memmove( void *dest, const void *src, size_t count );

#include <memory.h>
#include <string.h>
#include <stdio.h>

char string1[60] = "The quick brown dog jumps over the lazy fox";
char string2[60] = "The quick brown fox jumps over the lazy dog";
/*                           1         2         3         4         5
 *                  12345678901234567890123456789012345678901234567890
 */

void main( void )
{
   printf( "Function:\tmemcpy without overlap\n" );
   printf( "Source:\t\t%s\n", string1 + 40 );
   printf( "Destination:\t%s\n", string1 + 16 );
   memcpy( string1 + 16, string1 + 40, 3 );
   printf( "Result:\t\t%s\n", string1 );
   printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );

   /* Restore string1 to original contents */
   memcpy( string1 + 16, string2 + 40, 3 );

   printf( "Function:\tmemmove with overlap\n" );
   printf( "Source:\t\t%s\n", string2 + 4 );
   printf( "Destination:\t%s\n", string2 + 10 );
   memmove( string2 + 10, string2 + 4, 40 );
   printf( "Result:\t\t%s\n", string2 );
   printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );

   printf( "Function:\tmemcpy with overlap\n" );
   printf( "Source:\t\t%s\n", string1 + 4 );
   printf( "Destination:\t%s\n", string1 + 10 );
   memcpy( string1 + 10, string1 + 4, 40 );
   printf( "Result:\t\t%s\n", string1 );
   printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}


Output

Function:   memcpy without overlap
Source:      fox
Destination:   dog jumps over the lazy fox
Result:      The quick brown fox jumps over the lazy fox
Length:      43 characters

Function:   memmove with overlap
Source:      quick brown fox jumps over the lazy dog
Destination:   brown fox jumps over the lazy dog
Result:      The quick quick brown fox jumps over the lazy dog
Length:      49 characters

Function:   memcpy with overlap
Source:      quick brown dog jumps over the lazy fox
Destination:   brown dog jumps over the lazy fox
Result:      The quick quick brown dog jumps over the lazy fox
Length:      49 characters

 6.1mymemmove

void* mymemmove(void* dest, const void* src, size_t n) {
    // 类型转换,便于操作
    unsigned char* d = (unsigned char*)dest;
    const unsigned char* s = (const unsigned char*)src;
    
    // 如果目标和源相同,或者移动的字节数为 0,直接返回
    if (d == s || n == 0) {
        return dest;
    }

    // 处理内存重叠的情况
    if (d < s) {
        // 如果目标地址在源地址之前,正常从前到后复制
        for (size_t i = 0; i < n; i++) {
            d[i] = s[i];
        }
    } else {
        // 如果目标地址在源地址之后,反向复制
        for (size_t i = n; i > 0; i--) {
            d[i - 1] = s[i - 1];
        }
    }

    return dest;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值