编写一个标准strlen函数

来源:互联网 发布:java wait 和await 编辑:程序博客网 时间:2024/05/16 12:05

1、代码如下:

int MyStrlen(const char *strSrc)
{
    assert(strSrc != NULL);
    if (strSrc == NULL)
    {
        return 0;
    }
 
    int i = 0;
    while(*strSrc++ != '\0')
    {
        i++;
    }
 
    return i;
}

2、VS2010下的原版函数:

/****strlen - return the length of a null-terminated string**Purpose:*       Finds the length in bytes of the given string, not including*       the final null character.**Entry:*       const char * str - string whose length is to be computed**Exit:*       length of the string "str", exclusive of the final null byte**Exceptions:********************************************************************************/size_t __cdecl strlen (        const char * str        ){        const char *eos = str;        while( *eos++ ) ;        return( eos - str - 1 );}

3、另外,可以看看:编写一个标准strcpy函数

0 0