码迷,mamicode.com
首页 > 编程语言 > 详细

C语言实现strlen

时间:2014-09-09 10:34:18      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   div   sp   log   on   

 

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\n", *p);
12 
13     return (p - str - 1); // 最后p指向‘\0‘,所以需要减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\n", len);
16 }

 

C语言实现strlen

标签:style   blog   color   io   ar   div   sp   log   on   

原文地址:http://www.cnblogs.com/lingshaohu/p/3961191.html

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