2013-07-05 11:36:05

小结:

本函数给出了几种strlen的实现,有ugly implementation,也有good implementation。并参考标准库中的implementation,最后给出了比较好的implementation。

求字符串长度,可通过两种方式实现:

  1. 是在遍历字符串中字符的时候用一个计数器记录字符个数,如下面函数_strlen_1中所示;
  2. 可用指向字符串截尾的指针减去指向字符串开始的指针得到,这种方式写出的代码更加简洁,也是库函数采用的实现方式,如函数_strlen_2、_strlen_3、_strlen_4中采用的方式。

标准库函数并没有输入合法性检查,这将输入合法性检查的任务推给了函数的调用者。
对于strlen函数,好的implementation要考虑一下几点:

  1. 函数参数应为const;
  2. 返回值应为unsigned int;
  3. 注意输入合法性检查。

代码:

 #include <iostream>

 using namespace std;
#define SIZE 100 /***
*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:
*
*******************************************************************************/ //不好的implementation
//返回值应该用unsigned int或直接size_t,不应用int
int _strlen_1(const char *str) //参数要用const,防止改动
{
if (NULL == str)
{
return ;
} int len = ;
while (*str++ != '\0')
{
++len;
} return len;
} //使用指针计算长度,同时将返回值改为size_t类型
size_t _strlen_2(const char *str)
{
if (NULL == str)
{
return ;
} const char *pstr = str;
while (*pstr++ != '\0'); //循环结束时,pstr指向的是'\0'的下一个位置,而非'\0',因此下面要减1 //return (pstr - str + 1);
return (pstr - str - );
} //标准库函数给出的implementation
size_t _strlen_3 (
const char * str
)
{
const char *eos = str; while( *eos++ ) ; return( eos - str - );
} //标准库函数给出的implementation的改进,加上输入合法性检查
//好的implementation要考虑一下几点:
//1)函数参数应为const;
//2)返回值应为unsigned;
//3)注意输入合法性检查
size_t _strlen_4(const char *str) //typedef _W64 unsigned int size_t;
{
if (NULL == str) //标准库函数给出的implementation中没有判断字符串是否为空
{
return ;
} const char *eos = str; while ( *eos++ ); //等价于while (*eos++ != '\0'); return (eos - str - );
} int main()
{
//_strlen
char str[SIZE] = "hello world!"; cout<<"test _strlen_1..."<<endl;
cout<<"the length of string "<<str<<" is : "<<_strlen_1(str)<<endl;
cout<<"the length of string NULL is : "<<_strlen_1(NULL)<<endl; cout<<"test _strlen_2..."<<endl;
cout<<"the length of string "<<str<<" is : "<<_strlen_2(str)<<endl;
cout<<"the length of string NULL is : "<<_strlen_2(NULL)<<endl; cout<<"test _strlen_3(the standard implementation)..."<<endl;
cout<<"the length of string "<<str<<" is : "<<_strlen_3(str)<<endl;
/*cout<<"the length of string NULL is : "<<_strlen_3(NULL)<<endl;*/ cout<<"test _strlen_4..."<<endl;
cout<<"the length of string "<<str<<" is : "<<_strlen_4(str)<<endl;
cout<<"the length of string NULL is : "<<_strlen_4(NULL)<<endl; return ;
}

strlen的C/C+++实现的更多相关文章

  1. php的empty(),trim(),strlen()方法

    如果empty()函数的参数是非空或非零的值,则empty()返回FALSE.换句话说,"".0."0".NULL.array().var$var:以及没有任何 ...

  2. c/c++中关于sizeof、strlen的使用说明

    sizeof: 一般指类型.变量等占用的内存大小(由于在编译时计算,因此sizeof不能用来返回动态分配的内存空间的大小) strlen: c字符串的长度(参数必须是字符型指针 char*,当数组名作 ...

  3. [PHP源码阅读]strlen函数

    文章来自:http://www.hoohack.me/2016/02/22/phps-source-analytics-strlen 我在github有对PHP源码更详细的注解.感兴趣的可以围观一下, ...

  4. php每天一题:strlen()与mb_strlen()的作用分别是什么

    strlen()与mb_strlen()都是用于获取字符串长度的,那么它们两个有什么不同? strlen()与mb_strlen()的不同之处在于mb_strlen()第二个参数可以用于指定字符编码. ...

  5. sizeof与strlen的区别

    1 sizeof是操作符,而strlen是库函数: 2 sizeof的参数可以为任意变量或类型,而strlen必须以char*做参数,且字符串必须以‘/0’结尾: 3 数组名用作sizeof参数时不会 ...

  6. strlen()和sizeof()求数组长度

    在字符常量和字符串常量的博文里有提: 求字符串数组的长度 标准库函数strlen(s)可以返回字符串s的长度,在头文件<string.h>里. strlen(s)的判断长度的依据是(s[i ...

  7. Linux C 字符串函数 strlen()、strcat()、strncat()、strcmp()、strncmp()、strcpy()、strncpy() 详解

      strlen(返回字符串长度) 表头文件 #include <string.h> 定义函数 size_t strlen(const char *s); 函数说明 strlen()用来计 ...

  8. 回文字符串的判断!关于strlen(char * str)函数

    #include <stdio.h> #include <string.h> int ishuiw(char * p); int main() { ;//true-false接 ...

  9. 关于strlen误用的一点记录

    今天帮一个朋友查一个错误,是运行时报vector iterator incompatible,一般这种问题是向量和迭代器的类型不兼容,或者是进行迭代器判等时前后向量的结构发生变化,如erase操作之后 ...

  10. sizeof、strlen、字符串、数组,整到一块,你还清楚吗?

    写在前面 sizeof.strlen.字符串.数组,提到这些概念,相信学过C语言的人都能耳熟能详,也能谈得头头是道,但是,在实际运用中,当这些内容交织在一起时,大家却不一定能搞地清清楚楚,本文的目的正 ...

随机推荐

  1. Java多线程(三) 多线程间的基本通信

    多条线程在操作同一份数据的时候,一般需要程序去控制好变量.在多条线程同时运行的前提下控制变量,涉及到线程通信及变量保护等. 本博文主要总结:①线程是如何通信  ②如何保护线程变量 1.Java里的线程 ...

  2. 关于linux下零散的东西 --慢慢补充

    一.截图     ,使用Shift+Ctrl+PrtSc就可以截图. 二.tar命令参数 c:表示压缩 x:表示解压 z:表示gzip的方式解/压缩 j:表示bzip2的方式解/压缩 三.串口终端ke ...

  3. apache重写字段详细说明

    用Apache虚拟主机的朋友很多,apache提供的.htaccess模块可以为每个虚拟主机设定rewrite规则,这对网站SEO优化相当有用,同时也改善了用户体验.国内的虚拟机一般不提供.htacc ...

  4. jQuery学习教程(1)

    一.什么是jQuery JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safar ...

  5. 《C和指针》 读书笔记 -- 第10章 结构和联合

    1.聚合数据类型能够同时存储超过一个的单独数据,c提供了两种类型的聚合数据类型,数组和结构. 2.[1] struct SIMPLE { int a; }; struct SIMPLE x; [2] ...

  6. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  7. NYOJ 994 海盗分金 逆向递推

    链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=994 题意: 有n个海盗劫得了窖藏的m块金子,并准备瓜分这些战利品.按照古老流传下来的分金法则 ...

  8. 二、记一次失败的 CAS 搭建 之 证书配置

    ==================================================================================================== ...

  9. IOS game

    App Store真是个金矿,即使是红海一片,黑马也依旧不少,前有愤怒的小鸟,现在出了个Flappy Bird,虽然是去年推出的,但最近爆红App Store和Google Play,越南河内的独立游 ...

  10. IOS 学习教程

    IOS 学习教程http://www.gaixue.com/course/236#### 讲课http://wenku.baidu.com/view/6786064fe518964bcf847c63. ...