The implementation of the string function strlen (strcpy, strcat, strcmp)


foreword

This article mainly introduces the use of character functions and string functions and the corresponding implementation process, and further understands the related library functions of characters and strings in C language.


1. Find the length of the string

In C language, the strlen function is used to find the length of a string.

strlen

Its standard format is: size_t strlen ( const char * str ); The
parameter of this function is a character pointer modified by const, and its return value is an unsigned integer (size_t).
Pay attention to the following points during the use of this function:
(1) The length of the character string calculated by the strlen function does not include \0 .
That is, the string has '\0' as the end mark, and the strlen function returns the number of characters that appear before '\0' in the string.
(2) The character string to be solved must end with '\0'.

Example 1: Finding the length of a string

#include<stdio.h>
int main()
{
    
    

	char* a = "hfoervve";
	int ret = strlen(a);
	printf("%d\n", ret);
	return 0;
}

result:
insert image description here

Example 2: If the solution string does not contain \0 , that is:

#include<stdio.h>
int main()
{
    
    

	char a[] = {
    
    'a','b','c','d'};
	int ret = strlen(a);
	printf("%d\n", ret);
	return 0;
}

result:
insert image description here

The value is a random value (that is, the first \0 that appears after returning the array a)

Example 3: The return value of the strlen function is size_t (unsigned integer)

#include <stdio.h>
int main()
{
    
    
	const char* str1 = "abcdef";
	const char* str2 = "bbb";
	if (strlen(str2) - strlen(str1) > 0)
	{
    
    
		printf("str2>str1\n");
	}
	else
	{
    
    
		printf("srt1>str2\n");
	}
	return 0;
}

result:
insert image description here

Example 4: Simulation implementation of strlen function

#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strlen(const char* str)
{
    
    
	int count = 0;
	while (*str)
	{
    
    
		count++;
		str++;
	}
	return count;
}
int main()
{
    
    
	char* a = "jfoefnosdncosjf";
	int ret1=strlen(a);
	int ret2 = my_strlen(a);
	printf("%d\n", ret1);
	printf("%d\n", ret2);
	return 0;
}

result:
insert image description here


Second, the string copy function

strcpy

Its standard format is: char * strcpy(char * destination, const char * source );

The function parameters are divided into purpose string (char * destination) and source string (const char * source). Its return value is a character pointer type.
Pay attention to the following points during the use of this function:
(1) The source string must end with '\0'. That is to ensure the integrity of the copied string.
(2) The strcpy function will copy '\0' in the source string to the target space.
(3) The target space must be large enough to ensure that the source string can be stored.

Example 1: Copy a string

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	char a1[] = "vndfjvbdfivnkx";
	char  a2[] = "ndijifhons";
	strcpy(a1, a2);
	printf("%s\n", a1);
	return 0;
}

Result:
insert image description here
Example 2: Simulate the implementation of the string copy function

#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strcpy(char* a1, const char* a2)
{
    
    
	assert(a1 != NULL);
	assert(a2 != NULL);
	char* ret = a1;
	while (*a2 != '\0')
	{
    
    
		*a1++ = *a2++;
	}
	*a1 = *a2;
	return ret;//返回目的空间的起始地址
}
int main()
{
    
    
	char a1[] = "vndfjvbdfivnkx";
	char  a2[] = "ndijifhons";
	strcpy(a1, a2);
	printf("%s\n", a1);
	my_strcpy(a1, a2);
	printf("%s\n", a1);
	return 0;
}

result:
insert image description here


Third, the string append function

screwed up

Its standard format is: char * strcat ( char * destination, const char * source );

The function parameters are divided into purpose string (char * destination) and source string (const char * source). Its return value is a character pointer type.
Pay attention to the following points during the use of this function:
(1) The source string must end with '\0'.
(2) The target space must be large enough to accommodate the content of the source string.
(3) The target space must be modifiable.

Example 1: Implement string append

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	char a1[30] = "fnwieubicn";
	char a2[] = "ivni";
	strcat(a1, a2);
	printf("%s\n", a1);
	return 0;
}

result:
insert image description here

Example 2: Simulate the string append function

#include<stdio.h>
#include<string.h>
#include<assert.h>
//模拟实现strcat
char* my_strcat(char* b, char* a)
{
    
    
	char* ret = b;
	while (*a != '\0')
	{
    
    
		*b++ = *a++;
	}
	return ret;
};
int main()
{
    
    
	char a1[30] = "fnwieubicn";
	char a2[] = "ivni";
	strcat(a1, a2);
	printf("%s\n", a1);
	my_strcat(a1, a2);
	printf("%s\n", a1);
}

result:
insert image description here


Fourth, the comparison of strings

strcmp

Its standard format is: int strcmp (const char * str1, const char * str2 );

The function parameters are two strings, and its return value is an integer (int).
Pay attention to the following points during use of this function:
(1) If the first string is greater than the second string, a number greater than 0 will be returned.
(2) If the first string is equal to the second string, then return 0.
(3) If the first string is less than the second string, a number less than 0 will be returned.

Example 1: Implement string comparison

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	char* p1 = "vfdvbdb";
	char* p2 = "nevnoe";
	int ret = strcmp(p1, p2);
	printf("%d\n", ret);
	return 0;
}

result:
insert image description here

Example 2: Implementation of string comparison function

#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strcmp(const char* p1, const char* p2)
{
    
    
	assert(p1 != NULL && p2 != NULL);
	while (*p1 == *p2)
	{
    
    
		if (*p1 == '\0')
		{
    
    
			return 0;
		}
		p1++;
		p2++;
	}
	if (*p1 > *p2)
	{
    
    
		return 1;
	}
	else if (*p1 < *p2)
	{
    
    
		return -1;
	}
}
int main()
{
    
    
	char* p1 = "vfdvbdb";
	char* p2 = "nevnoe";
	int ret = strcmp(p1, p2);
	printf("%d\n", ret);
	int ret1 = my_strcmp(p1, p2);
	printf("%d\n", ret1);
	return 0;
}

result:
insert image description here


Summarize

That's all for this article.

Guess you like

Origin blog.csdn.net/m0_53689542/article/details/122628675