模拟实现strlen

来源:互联网 发布:中电云集 阿里云 编辑:程序博客网 时间:2024/06/05 19:35

模拟实现strlen

1.普通实现

#include <stdio.h>int my_strlen(const char *str){int count = 0;while(*str != '\0'){count++;str++;}return count;}

 

2.递归实现

int my_strlen(const char *str){if(*str == '\0')return 0;elsereturn 1+my_strlen(str+1);}
 
 

3.指针实现

int my_strlen(const char *str){char *start = str;while(*str){str++;}return str-start;}




 

 

0 0
原创粉丝点击