码迷,mamicode.com
首页 > 其他好文 > 详细

strcpy,strlen,strcmp实现

时间:2015-06-14 22:33:01      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

1.strcpy的实现

char * my_strcpy(char * s1, char * s2)
{
    assert(s1 != NULL&&s2 != NULL);
    char *res = s1;
    while ((*(res++) = *(s2++))!=\0);
    return res;
}

 

2.strlen的实现

int my_strlen(char * s)
{
    assert(s != NULL);

    int num = 0;
    while (*(s++))
        ++num;
    return num;
}

 

3.strcmp的实现

int my_strcmp(char * s1, char * s2)
{
    assert(s1 != NULL&&s2 != NULL);

    while (*s1 == *s2 && \0 != *s1 && \0 != *s2)
    {
        ++s1;
        ++s2;
    }

    if (*s1 == *s2)
        return 0;
    else if (*s1 > *s2)
        return 1;
    else
        return -1;
}

 

strcpy,strlen,strcmp实现

标签:

原文地址:http://www.cnblogs.com/acode/p/4575720.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!