zoukankan      html  css  js  c++  java
  • 求字符串长度函数实现的三种方法

    /*
    Date: 10/03/19 12:49
    Description: 求字符串长度函数实现的三种方法
    */

     1 #include<stdio.h>
     2 
     3 
     4 int strlen1(char *s);
     5 int strlen2(char *s);
     6 int strlen3(char *s);
     7 
     8 
     9 int main(void) 
    10 {
    11   char str[]="The function to test my length."; 
    12   printf("The length1 is:%d
    ",strlen1(str));
    13   printf("The length2 is:%d
    ",strlen2(str));
    14   printf("The length3 is:%d
    ",strlen3(str));
    15 
    16 }
    17 
    18 
    19 int strlen1(char *s)//设置计数器 
    20 {
    21   int count=0;
    22   while(*s!='')
    23   {
    24     s++;
    25     count++;
    26   }
    27   return count;
    28 }
    29 int strlen2(char *s)//指针减指针的方法 
    30 {
    31   char *p=s;
    32   while(*p!='')
    33   {
    34     p++;     
    35   }
    36   return p-s;
    37 }
    38 int strlen3(char *s)//利用函数递归的方法 
    39 {
    40   if(*s=='')
    41     return 0;
    42   else
    43     return 1+strlen3(s+1);
    44 }

    运行结果:

      

  • 相关阅读:
    初识SpringBoot
    Dubbo案例SSM整合
    Dubbo
    Spring错题整合
    SSM整合
    SpringMVC数据校验
    SpringMVC拦截器
    SpringMVC文件上传
    SpringMVC异常
    字符串
  • 原文地址:https://www.cnblogs.com/sinlearn/p/10504993.html
Copyright © 2011-2022 走看看