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; }
原文:http://760470897.blog.51cto.com/10696844/1706974