模拟实现字符串函数

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <string>
#include<assert.h>

char*  my_strstr(const char* str1, const char* str2)
{
    char* cp = (char*)str1;
    char* s1, * s2;
        if (!*str2)
        {
            return((char*)str1);
        }
        while (*cp)
        {
            s1 = cp;
            s2 = (char*)str2;
            while (*s2 && !(*s1 - *s2))
            {
                s1++, s2++;
            }
            if (!*s2)
                return(cp);
            cp++;
        }
        return(NULL);
}


void* my_memcpy(void* arr, const void* arr1, int count)

{

    void* ret = arr;

    assert(arr && arr1);

    char* _arr = (char*)arr;

    char* _arr1 = (char*)arr1;

    while (count--)

    {

        *_arr++ = *_arr1++;

    }

    return ret;

}


char* my_strcat(char* dest, const char* src)
{
    char* tmp = dest;

    while (*dest)
        dest++;
    while ((*dest++ = *src++) != '\0')
        ;

    return tmp;
}

int mystrcmp(const char* str1, const char* str2)
{
    int ret = 0;
    while (!(ret = *(unsigned char*)str1 - *(unsigned char*)str2) && *str1)
    {
        str1++;
        str2++;
    }
    if (ret < 0)
    {
        return -1;
    }
    else if (ret > 0)
    {
        return 1;
    }
    return 0;
}

char* mystrcpy(char* strDest, const char* strSrc)
{
    assert((strDest != NULL) && (strSrc != NULL)); 
    char* address = strDest;  
    while ((*strDest++ = *strSrc++) != '\0')
       ;
    return address;                     
}

int mystrlen(const char* str)
{
    assert(str != NULL);
    int len = 0;
    while ((*str++) != '/0')
        len++;
    return len;

}
上一篇:初识java的String类


下一篇:Java怎样创建字符串