Several methods of implementing strlen function in C language

Source: Internet
Author: User
Tags assert function prototype

Original address: http://www.51testing.com/html/72/n-221172.html

Legend is a common written question: 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 strlen (const char *p);

Thinking Analysis:

In a string, you can usually take advantage of the last Terminator ' \ ', but here the argument is const, read-only, then we can't beat his idea.

It is not possible to consume memory while the function is running, unless a register is used. "Do not use intermediate variables" only means that the programmer cannot display the application memory, that is, cannot have local variables or dynamic memory applications.

If a function automatically requests stack memory or uses a register to store variables, or uses an immediate number addressing constant, then it is equivalent to "Do not use intermediate variables".

From a function prototype, the return value is int, so it is necessary to store the value in a single place within the function, either as a constant or as a register. The length is not 1 can not be found at a time, it is necessary to have a recursive call, so the recursive function will automatically request the stack memory, so that the programmer "do not use intermediate variables." The value returned in the middle is automatically saved through the register, and is copied to int in the last return. The concept of temporary objects is also the object of the program, which is automatically applied by the compiler in the stack during the run, invisible to the programmer, and equivalent to "Do not use intermediate variables".

Another typical topic that does not apply for any variable is: reverse string

This is the problem of using constants, or the application of variables to the compiler in the recursive process automatically apply in the stack, that is, diehard.

No code, no truth, the simple source code is as follows:

#include <stdio.h>#include<string.h>#include<assert.h>intMystrlen (Const Char*str);intMyStrlen1 (Const Char*str);intMyStrlen2 (Const Char*str);intMain () {Char*str=NULL; STR="Hello jay!"; printf ("original strlen ():%d\n", strlen (str)); printf ("Mystrlen ():%d\n", Mystrlen (str)); printf ("myStrlen1 ():%d\n", MyStrlen1 (str)); printf ("myStrlen2 ():%d\n", MyStrlen2 (str));}intMystrlen (Const Char*STR)/*No intermediate variables, recursive implementation, easy to read*/{    if((str = = NULL) | | (*str = =' /') ) {        return 0; }    Else {        returnMystrlen (str+1)+1; }}intMyStrlen1 (Const Char*STR)/*Do not use intermediate variables, but also recursive implementation, write more concise*/{assert (str!=NULL); return*str? (MyStrlen1 (++STR) +1) :0;}intMyStrlen2 (Const Char*STR)/*an int variable was used*/{    if(Str==null)return 0; intLen =0;  for(; *str++! =' /'; ) {Len++; }    returnLen;}

Several methods of implementing strlen function in C language

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.