[C language] strlen() function

Table of contents

1. Introduction to the strlen() function

Second, the specific use of the strlen() function

3. Precautions for using the strlen function 

4. Three ways to realize the function of strlen

1. The way of the counter

2. Recursive way

3. The method of pointer minus pointer

Summarize


1. Introduction to the strlen() function

strlen function: calculates the length of the string str, traverses from the first address of the character, ends with '\0', and then returns the calculated length, which does not contain '\0'. Here is the strlen() function in the library:

size_t  strlen (const char* str);

  1. The parameters of the function are ------const char* str: character pointer
  2. The type of return value ------ size_t: unsigned integer (ie: unsigned int)

Prerequisite knowledge:

  • const keyword: It is used to define constants. If a variable is modified by the const keyword, the value of this variable cannot be changed. Therefore: the value of * str here cannot be modified.
  • size_t data type: represents the maximum length that any object in C can achieve, it is an unsigned integer.

   1. Use the const keyword: This is just to calculate the length of the string and modify it with const to avoid modifying the original string.

   2. Use the size_t data type: here returns the calculated string length, the minimum length is 0, and it cannot be negative.

Second, the specific use of the strlen() function

scene one:

#include <stdio.h>
#include <string.h>
int main()
{
	char arr[10] = "abcde";
	int num = strlen(arr);
	printf("数组arr的长度为:%d\n", num);

	return 0;
}

Output result:

 Scene two:

#include <stdio.h>
#include <string.h>

int main()
{
	char arr[] = { 'a','b','c','d','e' };
	int num = strlen(arr);
	printf("数组arr的长度:%d\n", num);

	return 0;
}

Output result: 

The output results of the above two scenarios are not the same

Why is there such a result, what should we pay attention to when using the strlen function?

3. Precautions for using the strlen function 

strlen function: When calculating the length, the calculation will stop only when '\0' is encountered, and the calculated length does not contain '\0'.

    Scenario 1, a string is stored in the arr character array (the string ends with '\0'), then when strlen traverses to the character 'e', ​​and then traverses backwards, it will encounter '\0' ', at this time strlen stops traversing and returns the number of characters: 5;

    Scenario 2: The five characters 'a', 'b', 'c', 'd', and 'e' are stored in the character array of arr in turn, and '\0' is not stored, so the 'e' character is stored after We don't know what's in there. The strlen function only stops when it encounters '\0', so the number returned is a random value.

Therefore: when we use the strlen function, we should check whether the character array ends with '\0'.

4. Three ways to realize the function of strlen

1. The way of the counter

code show as below:

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

int count_strlen(const char* p)
{
	assert(p != NULL);
	int count = 0;
	while (*p != '\0')
	{
		count++;
		p++;
	}
	return count;
}

int main()
{
	char arr1[] = "abcdefg";
	int num1=count_strlen(arr1);
	printf("字符串arr1的长度为:%d\n", num1);

	return 0;
}

Output result:

Supplementary knowledge: assert (assertion): is a macro.

The function of assert is to calculate the expression in parentheses, if its value is false (that is, 0), then it will print out an error message, and then terminate the operation of the program. The assert here is to prevent the pointer p from being NULL.

2. Recursive way

code show as below:

#include <stdio.h>
#include <assert.h>
int recursion_strlen(char* p)
{
    assert(p != NULL);
	if (*p != '\0')
	{
		return 1 + recursion_strlen(++p);
	}
	else
	{
		return 0;
	}
}
int main()
{
	char arr2[] = "abcdef";
	int num2 = recursion_strlen(arr2);
	printf("字符串arr2的长度为:%d\n", num2);

	return 0;
}

Output result:

3. The method of pointer minus pointer

code show as below:

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

int point_strlen(char* p)
{
	assert(p != NULL);
	char* start = p;
	while (*p != '\0')
	{
		p++;
	}
	return p - start;
}
int main()
{
	char arr3[] = "abcd";
	int num3 = point_strlen(arr3);
	printf("字符串arr3的长度为:%d\n", num3);

	return 0;
}

Output result:


Summarize

strlen() function: returns the number of characters before '\0', so it is necessary to standardize the initialization of the character array. 

Guess you like

Origin blog.csdn.net/weixin_47970952/article/details/126149644