Test interview 5 implement the C library function strlen

Source: Internet
Author: User

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

------------------------------------------------------------------




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.