您的位置:首页 > 职场人生

不使用任何辅助变量实现strlen(递归实现strlen)

2013-04-23 20:49 423 查看
/******************************************************
不允许调用库函数,也不允许使用任何全局或局部变量编写 int strlen(char *strDest)
******************************************************/
#include<iostream>
#include<time.h>
#include<cstring>
using namespace std;
int mystrlen(char *str)
{
return *(str++) ? mystrlen(str) + 1 : 0;
}

int main()
{
char a[] =
"If you don't take the time to attract a woman first,\
you won't give her a reason to want to even have a conversation with you.";
unsigned long start,stop;
start=time(NULL); //取值为秒
cout<<a<<endl;
stop=time(NULL);
cout<<"running time : "<<stop-start<<endl;
start=time(NULL); //取值为秒
cout<<"mystrlen(a)= "<<mystrlen(a)<<endl;
stop=time(NULL);
cout<<"running time : "<<stop-start<<endl;
cout<<"strlen(a)= "<<strlen(a)<<endl;
}
/******************
If you don't take the time to attract a woman first,     you won't give her a re
ason to want to even have a conversation with you.
running time : 0
mystrlen(a)= 130
running time : 0
strlen(a)= 130

Process returned 0 (0x0)   execution time : 0.919 s
Press any key to continue.

********************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息