strlen函数实现

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

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

方法1:利用中间变量

int strlen(const char *s){
    int i=0;
    while(s[i] != ''){
        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=='')  return 0;
    return (1+strlen(++s));
}

方法4:利用递归2

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

方法5:利用中间变量2

int strlen(char s[])  
{  
  int i;  
  while (s[i] != '')  
    ++i;  
  return i;  
}  
原文地址:https://www.cnblogs.com/litifeng/p/7465281.html