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

/******************************************************
不允许调用库函数,也不允许使用任何全局或局部变量编写 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.

********************


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值