/******************************************************
不允许调用库函数,也不允许使用任何全局或局部变量编写 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.
********************
不使用任何辅助变量实现strlen(递归实现strlen)
最新推荐文章于 2020-04-18 13:19:06 发布