Written question: Do not use intermediate variables to find the const string length, that is, the implementation of String length library function strlen function

Source: Internet
Author: User

Written questions:

Do not use intermediate variables to find the const string length, that is, the implementation of String length library function strlen function.

The function interface is declared as follows: int My_strlen (const char *p);


The actual function of the strlen function is to iterate from the first address representing the string until it encounters the Terminator ' \ S '.

The size of the returned length does not include ' + '.

#include <stdio.h> #include <assert.h>//uses intermediate variables//int my_strlen (const char *str)//{//assert (str);//int cou NT = 0;//while (*str++)//{//count++;//}//return count;//}//recursive method (do not use intermediate variable) int my_strlen (const char *STR) {if (*str = = ')} {return 0;} Else{return 1 + my_strlen (str + 1);}} int main () {char str[] = {0};gets (str); int len = My_strlen (str);p rintf ("%d\n", Len); System ("pause"); return 0;}


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/75/D6/wKioL1ZDSiaSCUhhAAApZBzGzpw986.png "title=" 3.png " alt= "Wkiol1zdsiascuhhaaapzbzgzpw986.png"/>


Written question: Do not use intermediate variables to find the const string length, that is, the implementation of String length library function strlen function

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.