温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

c语言实现My_string

发布时间:2020-04-09 20:13:39 来源:网络 阅读:414 作者:梦T醒 栏目:编程语言

实现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<stdio.h>
#include<assert.h>

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<stdio.h>
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<stdio.h>
#include<assert.h>

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<stdio.h>
#include<assert.h>

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<stdio.h>
#include<assert.h>

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<stdio.h>
#include<assert.h>

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;
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI