个人手撕代码二、字符串相关

一、strcpy

#include <iostream>
#include <string.h>

using namespace std;

//内存重叠的错误例子
char* my_strcpy_false(char* dst, const char* src,int cpylen)
{
    if (!dst || !src)
        return NULL;
    char* d = dst;
    int cnt = cpylen;
    while (*src != '\0'&&cnt!=0) {
        *d++ = *src++;
        cnt--;
    }
    *d = '\0';
    return dst;
}

char* my_strcpy(char* dst, const char* src)
{
    if (!dst || !src)
        return NULL;
    int size = strlen(src) + 1;
    char *d = dst;
    //内存覆盖
    if (src<d && d<src+size)
    {
        d += size - 1;
        src += size - 1;
        while (size > 0)
        {
            *d-- = *src--;
            size--;
        }
    }
    else
    {
        while (size > 0)
        {
            *d++ = *src++;
            size--;
        }
    }
    return dst;
}

int main()
{
    char dst[100];
    char src[100] = { "this is why we play." };
    my_strcpy(src, src);
    cout << src << endl;
    //cout << dst << endl;
    return 0;
}

二、memcpy和memmove

#include <iostream>
#include <string.h>

using namespace std;



void* my_memcpy(void* dst, const void* src,size_t n)
{
    if (!dst || !src)
        return NULL;
    char* d = (char*) dst;
    const char* s = (const char*) src;
    //内存覆盖
    if (s<d && d<s+n)
    {
        d += n - 1;
        s += n - 1;
        while (n--)
        {
            *d-- = *s--;
        }
    }
    else
    {
        while (n--)
        {
            *d++ = *s++;
        }
    }
    return dst;
}

int main()
{
    char dst[100];
    char src[100] = { "this is why we play." };
    memset(dst,'\0',sizeof(dst));
    my_memcpy(dst, src,10);
    cout << src << endl;
    cout << dst << endl;
    return 0;
}

三、strcmp

#include <iostream>
#include <assert.h>

using namespace std;

int my_strcmp(const char* dst, const char* src)
{
    assert(dst&&src);
    while (*dst == *src)
    {
        if (*dst == '\0')
            return 0;
        dst++;
        src++;
    }
    if (*dst > *src)
        return 1;
    else
        return -1;
}

int main()
{
    char dst[100] = { "where amazing happens." };
    char src[100] = { "this is why we play." };
    cout << my_strcmp(dst,src)<<endl;
    return 0;
}

四、strcat

#include <iostream>
#include <assert.h>

using namespace std;

void my_strcat(char* dst, const char* src)
{
    assert(dst&&src);
    //move to '\0'
    while (*dst != '\0')
    {
        ++dst;
    }
    while (*src != '\0')
    {
        *dst++ = *src++;
    }
    *dst = '\0';
}


int main()
{
    char dst[100] = { "where amazing happens." };
    char src[100] = { "this is why we play." };
    my_strcat(dst, src);
    cout << src << endl;
    cout << dst << endl;
    return 0;
}

五、strlen

#include <iostream>
#include <assert.h>

using namespace std;

size_t my_strlen(const char* src)
{
    if (!src)
        return 0;
    //assert(src);
    size_t len = 0;
    while (*src != '\0')
    {
        ++src;
        ++len;
    }
    return len;
}


int main()
{
    char dst[100] = { "where amazing happens." };
    char src[100] = { "this is why we play." };
    cout << my_strlen(dst) << endl;
    cout << my_strlen(src) << endl;
    return 0;
}

六、strstr

#include <iostream>
#include <assert.h>

using namespace std;

char* my_strstr(char* str1,char* str2)
{
    if (!str1 || !str2)
        return NULL;
    if (*str2 == '\0')
        return NULL;
    char* res=str1;
    while (*res != '\0')
    {
        char *s1 = res;
        char* s2 = str2;
        while (*s1 == *s2 && *s1 != '\0'&&*s2!='\0')
        {
            ++s1;
            ++s2;
        }
        //if s2 end first,ok
        if (*s2 == '\0')
            return res;
        //if s1 end first,false
        if (*s1 == '\0' && *s2 != '\0')
            return NULL;
        res++;
    }
    return NULL;
}


int main()
{
    char dst[100] = { "where amazing happens." };
    char src[100] = { "amazing" };
    cout << my_strstr(dst,src) << endl;
    return 0;
}

七、atoi

#include <iostream>
#include <assert.h>

using namespace std;

int my_atoi(const char* src)
{
    long long num=0;
    int sign=1;

    //skip space or others
    while ((*src==' ') || (*src=='\r') || (*src=='\n'))
    {
        ++src;
    }

    //get sign
    if (*src == '-')
    {
        sign = -1;
        src++;
    }
    else if (*src == '+')
    {
        sign = 1;
        src++;
    }


    //get number
    while (*src != '\0' && *src >= '0' && *src <= '9')
    { 
        long long tmp = *src;
        num = num * 10 + (tmp-'0');
        src++;
    }

    if (sign == -1)
        num = -num;
    return (int)num;

}

int main()
{
    char src[20] = { "  -31246" };
    cout << my_atoi(src) << endl;
    return 0;
}

八、itoa

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<algorithm>

using namespace std;

char* my_itoa(int num, char* str, int radix)
{
    
    //essential val
    char index[] = "0123456789ABCDEF";
    unsigned int u_num;
    int sign = 1;
    int str_idx=0;
    
    //radix ok?
    assert(radix >= 2 && radix <= 16);

    //if radix=10,
    if (radix == 10 && num < 0)
    {
        sign = -1;
    }

    u_num = (unsigned int)num;

    while (u_num != 0)
    {
        str[str_idx++] = index[u_num % radix];
        u_num /= radix;
    }

    //if radix=10 and sign=-1
    if (sign == -1)
        str[str_idx++] = '-';

    str[str_idx] = '\0';

    //now reverse all
    char* begin = str;
    char* end = str + str_idx - 1;
    while (begin < end)
    {
        swap(*begin, *end);
        ++begin;
        --end;
    }
    return str;
}

int main()
{
    char* buf = (char*) malloc(50);
    assert(buf);
    memset(buf, '\0', sizeof(buf));
    printf("255 = %s \r\n", my_itoa(255, buf, 16));
    memset(buf, 0, 50);
    printf("0xFF = %s \r\n", my_itoa(0xFF, buf, 10));
    free(buf);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值