C++ does not use variables to find string length strlen function
- 2020-04-02 03:06:48
- OfStack
In this paper, an example of C++ without using variables to calculate the string length strlen function. Share with you for your reference. The specific implementation method is as follows:
1, strlen source code implementation:
size_t strlen(const char *str)
//Strlen doesn't make memory invalid judgments. If it's NULL, it's core.
{
const char *eos=str;
while(*eos++);
return (eos-str-1);
}
2. Regular test questions will require no additional variables to achieve strlen function:
Implement a:
int strlen(const char *str)
{
if('0'==*str)
return 0;
else
return strlen(str+1)+1;
}
To realize two:
int strlen(const char *str)
{
return *str?(strlen(str+1)+1):0;
}
Hope that the article described in the C++ programming to help you.