[C] strlen of library function

Get string length

  Returns the length of the C string.

  The length of a C string is determined by the terminating null-character: A c string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

  The above content is the introduction of the strlen function on the C++ official website. It can be seen that the strlen function is used to calculate the length of the string, which is the same as the number of characters between the beginning of the string and the terminating null character (excluding the terminating null character itself).

  Next, four ways to implement the strlen function are given:

1. Create a temporary variable as a counter to count

/*
* Function name: MyStrlen
*
* Function: return the length of the C string
* Method 1: Create a temporary variable to count as a counter
*
* Entry parameter: str
*
* Exit parameter: length
*
* return type: int
*/

int MyStrlen(const char * str)
{
	int length = 0;

	assert(NULL != str);

	while ('\0' != *str)
	{
		length++;
		str++;
	}

	return length;
}

2. Pointer - Pointer

/*
* Function name: MyStrlen
*
* Function: return the length of the C string
* Method 2: Pointer-Pointer
*
* Entry parameter: start
*
* Exit parameters: end - start
*
* return type: int
*/

int MyStrlen(const char * start)
{
	const char * end = start;

	assert(NULL != start);

	while ('\0' != *end)
	{
		end++;
	}

	return (end - start);
}
3. Recursion
/*
* Function name: MyStrlen
*
* Function: return the length of the C string
* Method 3: Do not create temporary variables, use recursion
*
* Entry parameter: str
*
* Exit parameter: 0 or 1+MyStrlen(str+1)
*
* return type: int
*/

int MyStrlen(const char * str)
{
	assert(NULL != str);

	if ('\0' == *str)
	{
		return 0;
	}
	else
	{
		return (1 +	MyStrlen(str+1));
	}
}

4. One line of code implements strlen

  It can be achieved using comma expressions and ternary operators, which are actually recursive.

/*
* Function name: MyStrlen
*
* Function: Calculate the length of the string
*
* Entry parameter: str
*
* Exit parameter: 0 or 1 + MyStrlen(str + 1)
*
* return type: int
*/

int MyStrlen(const char * str)
{
	return assert(NULL != str), '\0' == *str ? 0 : 1 + MyStrlen(str + 1);
}

main function

#define _CRT_SECURE_NO_WARNINGS 1

/*
* Copyright (c) 2018, code farmer from sust
* All rights reserved.
*
* File name: MyStrlen.c
* Function: return the length of the C string,
* The length of a C string is the same as the number of characters between the beginning of the string and the terminating null character (excluding the terminating null character itself)
*
* Current version: V1.0
* Author: sustzc
* Completion date: April 20, 2018 13:54:49
*/

# include <stdio.h>
# include <assert.h>

int main(void)
{
	char *str = "abcdef";

	printf("string length: %d\n", MyStrlen(str));

	return 0;
}

output result


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325524632&siteId=291194637