Problem:
Evaluate the number (including spaces) of characters contained in a string, that is, evaluate the string length;
# Include <stdio. h> # include <assert. h> int _ strlen (const char * Str) {assert (STR! = NULL); int I = 0; For (; * STR ++! = '\ 0'; I ++); // For (; STR ++! = NULL; I ++); // some statements can also be used, but the execution result is an endless loop. Str ++ may not be null even if it crosses the border; return I ;} int _ strlen2 (const char STR []) {assert (STR! = NULL); int I = 0; while (STR [I]! = '\ 0') I ++; return I;} int main () {char TMP [] = "dsgrfr s00w e324 SD! "; // No matter TMP [] or TMP *, the string will end with '\ 0'; int lenth = _ strlen (TMP); int len2= _ strlen2 (TMP ); printf ("the string length1 is: % d \ n", lenth); printf ("the string leng2is: % d \ n", len22.); Return 0 ;}
The output is as follows:
[[Email protected] desktop] #./A. outthe string length1 is: 22The string leng2is: 22 [email protected] desktop] #