标签:
/* 法一:传入长度做参数 */ int strlen_x(const char * pstr,int &len) { char * pTemp = (char*)pstr; if (*pstr == ‘\0‘) return len; len++; pTemp = pTemp++; return strlen_x(pTemp, len); } /* 法二 不传入长度参数 http://blog.csdn.net/stpeace/article/details/8096629 */ int GetstrLen(char * pstr) { return *pstr ? GetstrLen(pstr + 1) + 1 : 0; } int _tmain(int argc, _TCHAR* argv[]) { char *pstr = "sadfjqaj"; int len = strlen(pstr); printf("strlen = %d\n",len); len = 0; len = strlen_x(pstr, len); printf("strlen_x = %d\n", len); len = GetstrLen(pstr); printf("GetstrLen = %d\n", len); return 0; }
两种写法思路一致,实现上我的第一种稍微麻烦了点
标签:
原文地址:http://my.oschina.net/Yuqingmu/blog/467526