補助変数を使用しない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.

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