strlen函数实现(局部变量实现和不用局部变量实现)

来源:互联网 发布:电动飞机杯 知乎 编辑:程序博客网 时间:2024/06/05 17:48

闲来无事,在网上看到一题要求不用局部变量实现strlen函数的题,就有了下面的成果。

 

 

// strlen函数实现.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<stdio.h>
#include<assert.h>

*strlen函数有局部变量实现*/
int strlen(const char* source)
 {
  assert(source != NULL);  //断言
  int length = 0;
  while (*source)
   {
    length++;
    source++;
   }
  return length;
 }
/*strlen函数递归(无变量)实现*/
 int strlen(const char* source)
  {
   assert(source != NULL);   //断言
   if(!*source) return 0;
   else
   return strlen(++source)+1;

  }
int _tmain(int argc, _TCHAR* argv[])
{
 int length = 0;
 char StrSource[20];
 scanf("%s",StrSource);
 length = strlen(StrSource);
 printf("这个字符串的长度是:%d",length);
 return 0;
}