实现字符串库函数

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 天前
Nginx单向链表 ngx_list_t
数据结构·nginx·链表·c
学习前端的小z7 天前
【C++】小乐乐求和问题的高效求解与算法对比分析
c
集大周杰伦9 天前
C语言指针与数组深入剖析及优化示例 指针解读 数组与指针的关系
c语言·c
学习前端的小z9 天前
【C++ 】for 循环系统深入解析与实现法比较
c
无为扫地僧10 天前
五、windows上vscode构建c/c++环境
c++·vscode·c
学习前端的小z10 天前
【C++】小乐乐求和题目分析n变量类型讨论
c
学习前端的小z10 天前
【C++】青蛙跳跃问题解析与解法
c
枫の准大一12 天前
【C++游记】Vector的使用和模拟实现
c
学习前端的小z14 天前
【C++】数的性质问题分析与优化
c
talentestors19 天前
Codeforces Round 992 (Div. 2) 解题报告
c++·c·题解·cpp