字符串-------库函数的实现

1.实现strcpy
拷贝函数char *strcpy
( char *strDestination目标, const char *strSource源 );

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strcpy(char* mb,const char* y)
{
    assert(y);
   assert(mb);
   char* top = mb;
    while (*mb++ = *y++)
    {
        ;
    }
    return top;
}
char a[] = "abcdef";
char b[10] = { 'g', 'h', 'l' };
int main()
{
    printf("%s\n",my_strcpy(a, b));
    //printf("%s\n",strcpy(a, b));//库函数,对照
    system("pause");
    return 0;
}

效果
这里写图片描述
2.实现strcat
拼接char *strcat
( char *strDestination目标, const char *strSource源 );

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<string.h>
char a[] = "abcdef";
char b[100] = { 'g','h','l','g','d'};
char* my_strcat(char* mb,const char*y)
{
    assert(mb);
    assert(y);
    char* top = mb;
    while (*mb)
    {
        mb++;
    }
    while (*mb++ = *y++)
    {
        ;
    }
    return top;
}
int main()
{
    //printf("%s\n",strcat(a, b));
    printf("%s\n",my_strcat(a, b));
    system("pause");
    return 0;
}

效果
这里写图片描述
3.实现strstr
查找char *strstr( const char *string, const char *strCharSet );

#include<stdio.h>
#include<assert.h>
char* my_strstr(const char * mubiao, const char * yuan)
{
    char* s1 = (char *)mubiao;
    char* s2 = (char *)yuan;
    char* flag1 = s1;
    char* flag2 = s2;
    assert(s1);
    assert(s2);
    if (*s2 == '\0')
        return NULL;
    while (*s1)
    {
        s1 = flag1;
        s2 = flag2;
        while (*s2)
        {
            if (*s1 == *s2)
            {
                s1++;
                s2++;
            }
            else
                break;
        }
        if (*s2 == 0)
            return flag1;
        flag1++;
    }
}
int main()
{
    printf("%s\n", my_strstr("abbbcde", "bbc"));
    system("pause");
    return 0;
}

效果
这里写图片描述
4.实现strchr
char *strchr( const char *string, int c );

#include<stdio.h>
#include<assert.h>
char *my_strchr(const char *mb, const char *yuan)
{
    assert(mb);
    assert(yuan);
    if (*yuan == '0')
        return NULL;
    char * s1 = mb;
    char * s2 = yuan;
    while (*s1)
    {
        if (*s1 == *s2)
            return s1;
        else
            s1++;
    }
}
int main()
{
    printf("%s\n", my_strchr("abbbbcef", "e"));
    system("pause");
    return 0;
}

效果
这里写图片描述
5.实现strcmp
int strcmp( const char *string1, const char *string2 );

#include<stdio.h>
#include<assert.h>
#include<string.h>
char a[] = "abcdef";
char b[100] = { 'a','b','c','d','e','f'};
int my_strcmp(const char* str1, const char* str2)
{
    assert(str1);
    assert(str2);
    while (*str1)
    {
        if (*str1 == *str2)
        {
            str1++;
            str2++;
        }
        else if (*str1 > *str2)
            return 1;
        else if (*str1<*str2)
            return -1;
    }
    return *str1;
}
int main()
{
    //printf("%d\n", strcmp(a, b));
    printf("%d\n",my_strcmp(a, b));
    system("pause");
    return 0;
}

效果
这里写图片描述
6.实现memcpy
void *memcpy( void *dest, const void *src, size_t count );

#include<stdio.h>
#include<string.h>
#include<assert.h>
void* my_memcpy(void * mb, void * y, size_t sz)
{
    assert(mb);
    assert(y);
    char* ret = (char *)mb;
    while (sz--)
    {
        *(char *)mb = *(char *)y;
        ++(char*)mb;
        ++(char*)y;
    }
    return ret;
}
char a[] = "abcdef";
char b[] = "ghijn";
int main()
{
    //printf("%s\n", memcpy(a,b,16));
    printf("%s\n", my_memcpy(a,b,3));
    system("pause");
    return 0;
}

这里写图片描述
7.实现memmove
void *memmove( void *dest, const void *src, size_t count );

#include<stdio.h>
#include<string.h>
#include<assert.h>
void * my_memmove(void * mb,const void * y, size_t sz)
{
    assert(mb);
    assert(y);
    char* ret = (char *)mb;
    if (mb < y)
    {
        char*ret = (char *)mb;
        while (sz--)
        {
            *(char *)mb = *(char *)y;
            ++(char*)mb;
            ++(char*)y;

        }
        return ret;
    }
    else if (mb > y)
    {
        char*ret = (char *)mb;
        while (sz--)
        {
            *((char*)mb + sz) = *((char*)y + sz);
        }
        return ret;
    }
}
int main()
{
    char a[] = "abcdef";
    //printf("%s\n", memmove(a,a+2,2));
    printf("%s\n", my_memmove(a,a+2,2));
    system("pause");
    return 0;
}

这里写图片描述

展开阅读全文

没有更多推荐了,返回首页