学C的同翅来看看吧

作者在 2011-08-16 12:00:33 发布以下内容
想必学过C语言的侠们都自己动手写过一些字符串操作相关的函数,
如strcpy,strcat,strcmp and so on!
现在试着按如下要求写strlen的个人版本,要求:
 1)、函数以字符串为参数,
2)、返回该字符串的长度,
3)、不能在函数内部定义任何变量





(想出来了就对照一下吧,底下就是:)




     参考函数:
int strLen(char* str)
{
    if(*str=='\0')
        return 0;
    return strLen(str+1)+1;
}
 
Cyuyan | 阅读 966 次
文章评论,共7条
尤慕思(作者)
2011-08-16 12:01
1
奇怪,函数头部怎么跑、外边去了?<img src="image/face/24.gif" class="face">
laznrbfe
2011-09-01 23:26
2
<div class="quote"><span class="q"><b>尤慕思</b>: 奇怪,函数头部怎么跑、外边去了?<img src="image/face/24.gif" class="face"></span></div><img src="image/face/2.gif" class="face">呵呵,真的耶,可能怕被束缚,所以跑了,呵呵~
embed_xuel
2011-09-21 00:31
3
int strLen(char* str)<br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; int n=0;<br />
&nbsp; &nbsp; &nbsp; &nbsp; while (*str++)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; n++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; return n;<br />
}
尤慕思(作者)
2011-09-21 17:28
4
<div class="quote"><span class="q"><b>embed_xuel</b>: int strLen(char* str)<br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; int n=0;<br />
&nbsp; &nbsp; &nbsp; &nbsp; while (*str++)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; n++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; return n;<br />
}</span></div>你的函数是对的,<br />
但要求是不在函数体内定义变量(你的整型变量n).
embed_xuel
2011-09-21 18:35
5
没看到要求,要是看到要求就不回了,没想到别的办法
leizisdu
2011-10-24 15:39
6
楼主用递归实现的,犀利<img src="image/face/17.gif" class="face">
leizisdu
2011-10-24 15:40
7
size_t strlen_personal(const char *str)<br />
{<br />
&nbsp; &nbsp; return strlen(str);<br />
} // <img src="image/face/22.gif" class="face">
游客请输入验证码
浏览68499次
最新评论