C语言实现strlen

strlen:

 1 #ifndef STRLEN_H
 2 #define STRLEN_H
 3 
 4 #include <stdio.h>
 5 
 6 // 参考微软的写法
 7 int cat_strlen(const char *str) {
 8     const char *p = str;
 9     
10     while (*p++) ;
11     //printf("%s
", *p);
12 
13     return (p - str - 1); // 最后p指向'',所以需要减1
14 }
15 
16 #endif

main:

 1 #include "strlen.h"
 2 
 3 void test_strlen();
 4 
 5 int main() {
 6     test_strlen();
 7 
 8     return 0;
 9 }
10 
11 
12 
13 void test_strlen() {
14     int len = cat_strlen("test strlen");
15     printf("%d
", len);
16 }
原文地址:https://www.cnblogs.com/lingshaohu/p/3961191.html