资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言实现My_string

实现strcpy strcat strstr strchr strcmp memcpy memove

创新互联建站坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站建设、做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的靖州网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

char my_strcpy(char *dest, const char *src)//实现strcpy
{
    assert(dest);
    assert(src);
    while (*src)
    {
        *dest = *src;
        dest++;
        src++;
    }
    *dest = '\0';
}

int main()
{
    char dest[20] ="1234567890";
    char src []= "abcdef";
    my_strcpy(dest, src);
    printf("%s\n", dest);
    system("pause");
    return 0;
}
#include
#include

char my_strcat(char *dest, const char *src)//实现strcat
{
    assert(dest);
    assert(src);
    while (*dest != '\0')
    {
        dest++;
    }
    while (*src != '\0')
    {
        *dest = *src;
        dest++;
        src++;
    }
}
int main()
{
    char dest[20] = "1234567890";
    char src []= "abcdef";
    my_strcat(dest, src);
    printf("%s", dest);
    system("pause");
    return 0;
}
#include
const char *Mystrchr(const char *src, char ch)//实现strchr
{
    while (*src != '\0')
    {
        if (*src == ch)
        {
            return src;
        }
        src++;
    }
    return NULL;
}
int main()
{
    char str[10] = "abcdefg";
    char a = 'b';
    printf("%s", Mystrchr(str,a));
    system("pause");
    return 0;
}
#include
#include

const char *My_strstr(const char *str1, const char *str2)实现strstr
{
    const char *s1 = NULL;
    const char *s2 = NULL;
    const char *start = str1;
    while (*start != '\0')
    {
        s1 = start;//s1回退到上一次的下一个位置
        s2 = str2;//s2回退到str2的起始位置
        while (*s1 != '\0' && *s2 != '\0'&&*s1 == *s2)
        {
            s1++;
            s2++;
        }
        if (*s1 == '\0')
        {
            return NULL;
        }
        if (*s2 == '\0')
        {
            return start;
        }
        start++;
    }
    return NULL;
}

int main()
{
    char dest[20] = "123aabcdef90";
    char src []= "abcdef";
    printf("%s\n", My_strstr(dest, src));
    system("pause");
    return 0;
}
#include
#include

int* my_strcmp(char *dest, const char *src)实现strcmp
{
    assert(dest);
    assert(src);
        while (*dest != '\0'&&*src!='\0')
        {
            if (*dest == *src)
            {
                dest++;
                src++;
            }
            else if (*dest > *src)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        return 0;
}
int main()
{
    char dest[] = "abcdef";
    char src []= "adcdef";
    int ret = my_strcmp(dest, src);
    printf("%d",ret);
    system("pause");
    return 0;
}
#include
#include

void *Mymemcpy(void *dest, const void *src, int count)//实现mencpy
{
    while (count != 0)
    {
        *((char*)dest) = *((char*)src);
        ((char*)dest)++;
        ((char*)src)++;
        count--;
    }
}

int main()
{
    int arr[10] = {1,2,3,4,5,6,7,8,9};
    int arr2[10] = {0};
    int i = 0;
    Mymemcpy(arr2,arr,16);
    for (; i < 10; i++)
    {
        printf("%d",arr2[i]);
    }
    system("pause");
    return 0;
}#include
#include

void *Mymemcpy(void *dest, const void *src, int count)//实现memcpy//count是字节数
{
    while (count != 0)
    {
        *((char*)dest) = *((char*)src);
        ((char*)dest)++;
        ((char*)src)++;
        count--;
    }
}

int main()
{
    int arr[10] = {1,2,3,4,5,6,7,8,9};
    int arr2[10] = {0};
    int i = 0;
    Mymemcpy(arr2,arr,16);
    for (; i < 10; i++)
    {
        printf("%d",arr2[i]);
    }
    system("pause");
    return 0;
}

当前题目:c语言实现My_string
路径分享:http://www.cdkjz.cn/article/jgjshi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220