标签:date include print font fun 函数实现 des cti 实现
/*
	Date: 10/03/19 12:49
	Description: 求字符串长度函数实现的三种方法
*/
#include<stdio.h> 
int strlen1(char *s);
int strlen2(char *s);
int strlen3(char *s);
int main(void) 
{
	  char str[]="The function to test my length."; 
	  printf("The length1 is:%d\n",strlen1(str));
	  printf("The length2 is:%d\n",strlen2(str));
	  printf("The length3 is:%d\n",strlen3(str));
	
}
int strlen1(char *s)//设置计数器 
{
	  int count=0;
	  while(*s!=‘\0‘)
	  {
		    s++;
		    count++;
	  }
	  return count;
}
int strlen2(char *s)//指针减指针的方法 
{
	  char *p=s;
	  while(*p!=‘\0‘)
	  {
		    p++;	 
	  }
	  return p-s;
}
int strlen3(char *s)//利用函数递归的方法 
{
	  if(*s==‘\0‘)
		    return 0;
	  else
		    return 1+strlen3(s+1);
}
运行结果:
  
标签:date include print font fun 函数实现 des cti 实现
原文地址:https://www.cnblogs.com/sinlearn/p/10504993.html