C语言中的strlen实现方法

来源:互联网 发布:大数据 crm 系统架构 编辑:程序博客网 时间:2024/06/08 07:34

最近想实现一下strlen, 写了如下代码

int mystrlen(const char * str){    assert(str != NULL);    const char * pstr = str;    //get length    int length = 0;    while (*pstr++ && ++length);    //printf("length = %d, %d\n", length, strlen(str));    return length;}

本来以为已经是蛮简洁的了, 结果一看标准库, 发现更简单<-_->!!!

/****strlen.c - contains strlen() routine**       Copyright (c) Microsoft Corporation. All rights reserved.**Purpose:*       strlen returns the length of a null-terminated string,*       not including the null byte itself.********************************************************************************/#include <cruntime.h>#include <string.h>#pragma function(strlen)/****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 );}

放在这里算是一个记录吧<^_^>

0 0
原创粉丝点击