实现字符串库函数

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;
}
相关推荐
绯樱殇雪4 小时前
函数题 6-8 简单阶乘计算【PAT】
c·pat考试
伏城之外1 天前
LeetCode - 17 电话号码的字母组合
java·javascript·c++·python·leetcode·c
TT-Kun4 天前
算法 | 基础排序算法:插入排序、选择排序、交换排序、归并排序
算法·排序算法·c
Cyan_RA95 天前
C 408—《数据结构》算法题基础篇—链表(上)
java·数据结构·算法·链表·c·408·计算机考研
Artintel7 天前
[学习笔记]《CSAPP》深入理解计算机系统 - Chapter 6 存储器层次结构
笔记·学习·c·csapp
一丝晨光7 天前
安全API
java·开发语言·c++·安全·编程·c·编程语言
伏城之外9 天前
LeetCode - 15 三数之和
java·javascript·c++·python·leetcode·c
终末圆9 天前
Swiper轮播图框架【前端 24】
前端·javascript·数据结构·c++·python·算法·c
终末圆11 天前
使用C语言实现字符推箱子游戏
c语言·开发语言·数据结构·算法·游戏·排序算法·c
打鱼又晒网17 天前
linux文件——文件系统——学习、理解、应用软硬件链接
linux·服务器·数据库·后端·操作系统·c