Write Functions in C language to implement strlen's function of calculating the string length
This article introduces three methods: 1. Cyclic notation (set a counter ). 2. Recursive Method, (function call itself for calculation) 3. pointer-pointer method (library function uses this method) is now included in the program: Method 1:
/* Counting Method */int my_strlen (char * p) {int number = 0; while (* p) {number ++; p ++;} return number ;}
Method 2:
/* Recursion */int my_strlen (char * str1) {if (* str1! = '\ 0') {str1 ++; return 1 + my_strlen (str1) ;}else return 0 ;}
Method 3:
Int main () {char * str = "asdfg"; int len = my_strlen (str); printf ("% d \ n", len); system ("pause "); return 0 ;}
Now we provide the main function for calling and testing:
Int main () {char * str = "asdfg"; int len = my_strlen (str); printf ("% d \ n", len); system ("pause "); return 0 ;}
After verification, all results are 5, and the calculation result is correct! If any great God finds that the program is still to be improved, please criticize and correct it!