1.指针
#include<stdio.h>
int strlen(char s[])
{
int len=0;
while(*s++!='\0')
{
len++;
}
return len;
}
int main()
{
char s[]="123456789";
printf("%d\n",strlen(s));
system("pause");
return 0;
}
2.计数
#include<stdio.h>
int strlen(char s[])
{
int i=0;
int count=0;
while(s[i++]!='\0')
{
count++;
}
return count;
}
int main()
{
char s[]="123456789";
printf("%d\n",strlen(s));
system("pause");
return 0;
}
3.递归
#include<stdio.h>
int strlen(char s[])
{
if(*s=='\0')
return 0;
else
return 1+strlen(s+1);
}
int main()
{
char s[]="abcdef";
printf("%d\n",strlen(s));
system("pause");
return 0;
}
标签:return,int,char,++,三种,printf,字符串,长度,strlen
From: https://blog.51cto.com/u_14682436/6178762