Problem:
Three methods for finding a string
I. Method of counting
#include <stdio.h> #include <assert.h>int my_strlen (char* str) {int Count=0;while (*STR) {count++;str++;} return count;} int main (void) {char *arr = "ABCEF"; int ret = My_strlen (arr);p rintf ("%d\n", ret);}
Second, the pointer-pointer method
#include <stdio.h> #include <assert.h>int my_strlen (const CHAR*STR) {assert (SRT); const CHAR* RET = Str;while (*ret++) {;} return (RET-STR-1);} int main (void) {char *arr = "abcdef"; printf ("%d\n", My_strlen (arr));}
Third, the method of recursion
#include <stdio.h> #include <assert.h>int my_strlen (char* srt) {assert (SRT), if (*srt = = ') {return 0;} else{Srt++;return (1 + my_strlen (SRT));}} int main (void) {char *arr = "abcdef";p rintf ("%d\n", My_strlen (arr));}
This article is from the "Soul of the Program" blog, so be sure to keep this source http://9195095.blog.51cto.com/9185095/1706394
C Language--three methods to find the length of a string