Test interview 5 implement the C library function strlen
This is a very simple problem.
Strlen (str) is used to calculate the total number of characters in str.
When storing strings, A '\ 0' is added to the end of the last character '.
Use this to write your own strlen function.
Source code:
# Include
# Include
Int MyStrlen (char str []) {if (str = NULL) // judge whether str is valid return 0; int counts = 0; // directly use counts as the counter while (str [counts ++]! = '\ 0'); return counts-1;} int main () {char str1 [] = "hello"; // 5 char * str2 = "OK "; // 2 char * str3 = ""; // 0 char * str4; // 0 printf ("str1 = % s \ n", str1 ); printf ("strlen (str1) = % d \ n", MyStrlen (str1); printf ("str2 = % s \ n", str2 ); printf ("strlen (str2) = % d \ n", MyStrlen (str2); printf ("str3 = % s \ n", str3 ); printf ("strlen (str3) = % d \ n", MyStrlen (str3); printf ("str4 = % s \ n", str4 ); printf ("strlen (str4) = % d \ n", MyStrlen (str4); getch ();}
Test results:
------------------------------------------------------------------
// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.
Reprinted please indicate the source: http://blog.csdn.net/qq844352155
Author: unparalleled
Email: coderguang@gmail.com
Yu GDUT
------------------------------------------------------------------