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

strlen函数实现

时间:2017-09-01 23:11:55      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:函数实现   方法   null   blog   code   ons   中间   作用   style   

原型: int strlen(const char *s); 

作用:返回字符串的长度。

方法1:利用中间变量

int strlen(const char *s){
    int i=0;
    while(s[i] != \0){
        i++;
    }
    return i;
}

方法2:利用指针

int strlen(const char *s){
    char *t=s;while(*s){
    s++;
  }
  return s-t; }

方法3:利用递归

int strlen(const char *s){
    if(s==NULL) return -1;
    if(*s==\0)  return 0;
    return (1+strlen(++s));
}

方法4:利用递归2

int strlen(const char *s){
    if(s==NULL)    return -1;    
    return    (\0 != *s)?(1+strlen(++s):0;
}

方法5:利用中间变量2

int strlen(char s[])  
{  
  int i;  
  while (s[i] != \0)  
    ++i;  
  return i;  
}  

 

strlen函数实现

标签:函数实现   方法   null   blog   code   ons   中间   作用   style   

原文地址:http://www.cnblogs.com/litifeng/p/7465281.html

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