zoukankan      html  css  js  c++  java
  • strlen的C/C+++实现

    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. 注意输入合法性检查。

    代码:

      1 #include <iostream>
      2 
      3 using namespace std;
      4 #define SIZE 100
      5 
      6 /***
      7 *strlen - return the length of a null-terminated string
      8 *
      9 *Purpose:
     10 *       Finds the length in bytes of the given string, not including
     11 *       the final null character.
     12 *
     13 *Entry:
     14 *       const char * str - string whose length is to be computed
     15 *
     16 *Exit:
     17 *       length of the string "str", exclusive of the final null byte
     18 *
     19 *Exceptions:
     20 *
     21 *******************************************************************************/
     22 
     23 //不好的implementation
     24 //返回值应该用unsigned int或直接size_t,不应用int
     25 int _strlen_1(const char *str)    //参数要用const,防止改动
     26 {
     27     if (NULL == str)
     28     {
     29         return 0;
     30     }
     31 
     32     int len = 0;
     33     while (*str++ != '')
     34     {
     35         ++len;
     36     }
     37 
     38     return len;
     39 }
     40 
     41 //使用指针计算长度,同时将返回值改为size_t类型
     42 size_t _strlen_2(const char *str)
     43 {
     44     if (NULL == str)
     45     {
     46         return 0;
     47     }
     48 
     49     const char *pstr = str;
     50     while (*pstr++ != '');  //循环结束时,pstr指向的是''的下一个位置,而非'',因此下面要减1
     51 
     52     //return (pstr - str + 1);
     53     return (pstr - str - 1);
     54 }
     55 
     56 //标准库函数给出的implementation
     57 size_t  _strlen_3 (
     58     const char * str
     59     )
     60 {
     61     const char *eos = str;
     62 
     63     while( *eos++ ) ;
     64 
     65     return( eos - str - 1 );
     66 }
     67 
     68 
     69 //标准库函数给出的implementation的改进,加上输入合法性检查
     70 //好的implementation要考虑一下几点:
     71 //1)函数参数应为const;
     72 //2)返回值应为unsigned;
     73 //3)注意输入合法性检查
     74 size_t _strlen_4(const char *str)   //typedef _W64 unsigned int   size_t;
     75 {
     76     if (NULL == str)      //标准库函数给出的implementation中没有判断字符串是否为空
     77     {
     78         return 0;
     79     }
     80 
     81     const char *eos = str;
     82 
     83     while ( *eos++ );  //等价于while (*eos++ != '');
     84 
     85     return (eos - str - 1);
     86 }
     87 
     88 int main()
     89 {
     90     //_strlen
     91     char str[SIZE] = "hello world!";
     92 
     93     cout<<"test _strlen_1..."<<endl;
     94     cout<<"the length of string "<<str<<" is : "<<_strlen_1(str)<<endl;
     95     cout<<"the length of string NULL is : "<<_strlen_1(NULL)<<endl;
     96 
     97     cout<<"test _strlen_2..."<<endl;
     98     cout<<"the length of string "<<str<<" is : "<<_strlen_2(str)<<endl;
     99     cout<<"the length of string NULL is : "<<_strlen_2(NULL)<<endl;
    100 
    101     cout<<"test _strlen_3(the standard implementation)..."<<endl;
    102     cout<<"the length of string "<<str<<" is : "<<_strlen_3(str)<<endl;
    103     /*cout<<"the length of string NULL is : "<<_strlen_3(NULL)<<endl;*/
    104 
    105     cout<<"test _strlen_4..."<<endl;
    106     cout<<"the length of string "<<str<<" is : "<<_strlen_4(str)<<endl;
    107     cout<<"the length of string NULL is : "<<_strlen_4(NULL)<<endl;
    108 
    109     return 0;
    110 }
  • 相关阅读:
    分布式锁获取token
    美团-2019Q2述职总结
    linux df 日志删除命令分析
    MySQL、HBase、ES的特点和区别
    C++函数返回局部变量
    C语言 数组初始化的三种常用方法({0}, memset, for循环赋值)以及原理
    C Mysql API连接Mysql
    C++运算符重载
    C++对象赋值的四种方式
    C/C++下scanf的%匹配以及过滤字符串问题
  • 原文地址:https://www.cnblogs.com/youngforever/p/3173432.html
Copyright © 2011-2022 走看看