Do not use library functions, or even use variables to write strlen functions, or other functions of strings are a common problem encountered during the test and interview.
Post belowCodeShare.
# Include <stdio. h> # include <stdlib. h> # include <string. h> int my_strlen1 (const char * Str) // recursion {return ('\ 0 '! = * Str )? (1 + my_strlen1 (++ Str): 0;} int my_strlen2 (const char * Str) // iteration {int COUNT = 0; while (* Str! = '\ 0') {count ++; STR ++;} return count ;}int main () {char * P; P = (char *) malloc (100 * sizeof (char); While (scanf ("% s", P )! = EOF) {int count1 = 0, count2 = 0, count3 = 0; count1 = my_strlen1 (p); count2 = my_strlen2 (p); count3 = strlen (P ); printf ("count1 = % d, count2 = % d, coont3 = % d \ n", count1, count2, count3);} Free (p); Return 0 ;}
The my_strlen1 function uses a recursive policy without any variables. When writing recursion, you must consider the recursive depth, which may sometimes cause stack overflow.
The my_strlen2 function uses simple iterations and is easy to understand.
There are other types of functions, such as writing two string-connected functions and string-Compared functions without using database functions.