C language - string function (detailed explanation in 7,000 words)

Table of contents

1. String function

String functions with unlimited length

1.1 shot

Precautions:

Use of strlen function

 Simulation implementation of strlen function

 1.2strcpy

Precautions:

Use of strcpy function

 Simulation implementation of strcpy function

 1.3strcat

Precautions

Use of strcat function

Simulation implementation of strcat function 

1.4strcmp

Return value of strcmp

Use of strcmp function

Simulation implementation of strcmp function

Limited-length string functions

1.5strncpy

Precautions

Use of strncpy function

 1.6strncat

Precautions:

Use of strncat function

 1.7 strncmp

Precautions:

Use of strcamp function

 String search

1.8strstr

Precautions:

Use of strstr function

 Simulation implementation of strstr function

1.9strtok

Precautions:

Use of strtok function

Edit

 Error message reporting

1.10strerror

Precautions:

Use of strerror function

 Character classification function

Character conversion:

Use of character segmentation functions and character conversion


 If you have any questions or opinions, please leave a message in the comment area.

1. String function

1.1 shot

1.2strcpy

1.3strcat

1.4strcmp

1.5strncpy

1.6strncat

1.7 strncmp

1.8strstr

1.9strtok

1.10strerror

String functions with unlimited length

1.1 shot

The strlen function is used to find the length of a string.

size_t strlen (const char * str); (string) const means that the content pointed by the pointer cannot be changed

Precautions:

The string uses '\0' as the end mark, and the strlen function returns the number of characters before '\0' (excluding '\0').

The string pointed to by the parameter must end with '\0'.

The function return value size_t is essentially unsigned int. (rename typedef unsigned int size_t)

Use of strlen function

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

int main()
{
	char arr1[] = { 'a','b','c' };
	printf("%d\n", strlen(arr1));//错误使用,strlen以'\0'作为结束标志,而在arr1中不知道'\0'在什么位置,结果是一个随机值
	printf("%d\n", sizeof(arr1));

	char arr2[] = { "abcdef" };
	printf("%d\n", strlen(arr2));//strlen不统计'\0'
	printf("%d\n", sizeof(arr2));//sizeof会统计'\0'

	return 0;
}

224323765d594e6799a25c6e120d9c1e.png

 Simulation implementation of strlen function

3 methods to implement: counting, pointer arithmetic, recursion

#include<stdio.h>
#include<stdlib.h>

//数数
size_t de_strlen1(const char* arr)
{
	int count = 0;
	while (*arr != '\0')
	{
		count++;//过一位,数一个
		arr++;
	}
	return count;
}

//指针-指针(得到的值是它们之间的元素个数)
size_t de_strlen2(const char* arr)
{
	const char* arr1 = arr;
	while (*arr != '\0')
	{
		arr++;//放在while中会多移一位
	}
	return arr- arr1;
}

//递归
size_t de_strlen3(const char* arr)
{
	if (*arr != '\0')
	{
		return 1 + de_strlen3(arr + 1);
	}
	else
		return 0;
}


int main()
{
	char arr[] = { "abcdef" };
	int re1=de_strlen1(arr);
	printf("%d\n", re1);

	int re2 = de_strlen2(arr);
	printf("%d\n", re2);

	int re3 = de_strlen3(arr);
	printf("%d\n", re3);
	return 0;
}

479f99612acb40928f54f09cfbffc937.png

 1.2strcpy

strcpy is a string copy function.

char * strcpy ( char * destination, const char * source ); (destination space, source string)

Precautions:

The source string must end with '\0'.

At the same time, the '\0' in the source string will also be copied to the target space.

The destination space must be large enough to hold the source string.

The target space must be variable. (not a constant)

Use of strcpy function

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

int main()
{
	char arr1[10] = { "home" };
	char arr2[5] = { "ouse" };
	strcpy(arr1+1, arr2);
	printf("%s", arr1);

	return 0;
}

7fa5a0601b5d4819b86c3d2ce1d5667e.png

 Simulation implementation of strcpy function

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

char*  de_strcpy(char* dest, const char* src)
{
	char* re = dest;//记录初始地址
	while (*dest++ = *src++)//++后置先运算后++,直到遇到'\0',循环结束。
	{
		;
	}
	return re;
}

int main()
{
	char arr1[10] = { "home" };
	char arr2[5] = { "ouse" };
	de_strcpy(arr1+1, arr2);
	printf("%s", arr1);

	return 0;
}

a6a9cbe6938d489e91264258b4cbb04f.png

 1.3strcat

strcat is a string append function

char * strcat (char * destination, const char * source); (destination space, source string)

Precautions

The source string must end with '\0'.

The destination space must be large enough to hold the source string.

Goals must be variable.

strcat cannot append itself. (will overwrite own '\0')

Use of strcat function

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

int main()
{
	char arr1[20] = { "hello " };
	char arr2[10] = { "world" };
	strcat(arr1, arr2);
	printf("%s", arr1);

    strcat(arr2,arr2)//错误

	return 0;
}

686eb67ed8d043deb93b4602da45cf80.png

Simulation implementation of strcat function 

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

char* de_strcat(char* dest, const char* src)
{
	char* re = dest;
    1、找到目标空间的'\0'
	while (*dest)//切记不能把++放在这,因为循环结束后,++指针会调到'\0'后面一位
	{
		dest++;
	}
    2、追加
	while (*dest++ = *src++)
	{
		;
	}
	return re;
}

int main()
{
	char arr1[20] = { "hello " };
	char arr2[10] = { "world" };
	de_strcat(arr1, arr2);
	printf("%s", arr1);

	return 0;
}

1.4strcmp

The strcmp function compares the size of two strings.

int strcmp ( const char * str1, const char * str2 ) (string 1, string 2)

Return value of strcmp

>0 string1>string2

=0 string1>string2

<0 string1<string2

Use of strcmp function

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

int main()
{
	char arr1[] = { "abcd" };
	char arr2[] = { "abcdef" };
	char arr3[] = { "qq" };
	int re1 = strcmp(arr3, arr2);
	int re2 = strcmp(arr3, arr3);
	int re3 = strcmp(arr1, arr2);
	printf("%d\n", re1);
	printf("%d\n", re2);
	printf("%d\n", re3);

	return 0;
}

Simulation implementation of strcmp function

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

int de_strcmp(const char* str1, const char* str2)
{
   //判断两个字符串是否相等
	while (*str1 == *str2)
	{
		if (*str1 == '\0')
		{
			return 0;
		}
		str1++;
		str2++;
	}
    //比较字符的ascll码值
	return *str1 - *str2;
}

int main()
{
	char arr1[] = { "abcd" };
	char arr2[] = { "abcdef" };
	int re = de_strcmp(arr1, arr2);
	printf("%d", re);
	

	return 0;
}

You’ve all seen this, isn’t xdm considering a free three-way wave?

Take a break, continued from above

Limited-length string functions

1.5strncpy

char * strncpy ( char * destination, const char * source, size_t num ); (destination space, source string, number of copies)

Precautions

Copy num strings to the target space.

If the length of the source string is less than num, after copying the source string, append 0 to the end of the target until num.

Use of strncpy function

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

int main()
{
	char arr1[10] = {"hello"};
	char arr2[10] = { "ry" };
	strncpy(arr1, arr2, 2);
	printf("%s\n", arr1);

	char arr3[10] = { "hello" };
	char arr4[10] = { "ry" };
	strncpy(arr3, arr4, 3);
	printf("%s\n", arr3);
	return 0;
}

8d792b547c3249588d99f1cfa9c45e69.png

 1.6strncat

char * strncat (char * destination, const char * source, size_t num); (destination space, source string, append number)

Precautions:

Append num strings to the target space, plus one '\0'.

If the length of the C string is less than num, only copy before '\0' Content.

Use of strncat function

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

int main()
{
	char arr1[10] = { "ha" };
	char arr2[5] = { "hahe" };
	strncat(arr1, arr2, 2);
	printf("%s", arr1);

	return 0;
}

f0d0e6a9fbb84408949d324c68cf87f8.png

 1.7 strncmp

int strncmp ( const char * str1, const char * str2, size_t num );

Precautions:

Compare until different characters appear or one character ends or all num characters are compared.

The return value has the same meaning as strcmp.

Use of strcamp function

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

int main()
{
	char arr1[] = { "abcd" };
	char arr2[] = { "ab" };
	int re1=strncmp(arr1, arr2, 2);
	int re2 = strncmp(arr1, arr2, 3);
	printf("%d\n", re1);
	printf("%d\n", re2);

	return 0;
}

142f0bb4dce64ac69256cd024602532c.png

 String search

1.8strstr

The stratr function is used to find substrings

const char * strstr ( const char * str1, const char * str2 ); (string, substring)

Precautions:

Returns a pointer to the first occurrence of str2 in str1 if str2 is not a> str1 will return a null pointer.

The matching process does not include '\0', but ends with it.

Use of strstr function

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

int main()
{
	char arr1[]= {"abcdefg"};
	char arr2[]= {"bcd"};
	char arr3[]= {"cbd"};
	char* re1 = strstr(arr1, arr2);
	char*re2=strstr(arr1, arr3);
	if (re1 == NULL)
	{
		printf("找不到子字符串\n");
	}
	else
	{
		printf("%s\n", re1);
	}
	if (re2 == NULL)
	{
		printf("找不到子字符串\n");
	}
	else
	{
		printf("%s\n", re2);
	}
	return 0;
}

31bbdcdf31d6467b996b34a1af813d88.png

 Simulation implementation of strstr function

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

char* de_strstr(const char* str1, const char* str2)
{
	const char*re = str1;
	const char* s2 = str2;
	const char* s1= str1;
	if (str2 == NULL)
	{
		return (char*)str1;
	}
	while (*re)
	{
		 s2 = str2;
		s1=re;
		while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
		{
				s1++;
				s2++;
		}
		if (*s2 == '\0')
		{
			return (char*)re;
		}
		re++;
	}
		return NULL;
}

int main()
{
	char arr1[10]= {"abbcdefg"};
	char arr2[4]= {"bcd"};
	char arr3[4]= {"cbd"};
	char* re = de_strstr(arr1, arr2);
if (re== NULL)
	{
		printf("找不到子字符串\n");
	}
	else
	{
		printf("%s\n", re);
	}
	return 0;
}

1.9strtok

char * strtok (char * str, const char * delimiters); (split string, string containing delimiting characters)

Precautions:

The parameter is a string that defines the set of characters used as separators.

The first parameter specifies a string containing zero or more tokens separated by one or more delimiters in the sep string.

The strtok function finds the next token in str and ends it with \0, Returns a pointer to this tag. (Note:
The strtok function will change the string being operated on, so when using the strtok function to split Strings are generally the contents of temporary copies
And can be modified. )
strtok The first parameter of the function is not NULL , the function will find str The first tag in strtok The function will save it in the string
location in.
strtok The first parameter of the function is NULL , the function will be in the same string Starting from the saved position, search for the next index
remember.
If there are no more tokens in the string, a NULL pointer is returned.

Use of strtok function

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

int main()
{

	char arr[] = "192#168.120.85";
	char* p = "#.";
	char buf[20] = { 0 };
	strcpy(buf, arr);
	char* ret = NULL;
	for (ret = strtok(buf, p); ret != NULL; ret = strtok(NULL, p))
	{
		printf("%s\n", ret);
	}
	return 0;
}

 error message report

1.10strerror

The strerror function is used to obtain error information        

char * strerror ( int errnum )

Precautions:

Return error code and corresponding error message

Use of strerror function

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

int main()
{
	printf("%s\n", strerror(1));
	printf("%s\n", strerror(2));
	printf("%s\n", strerror(3));
	printf("%s\n", strerror(4));
	printf("%s\n", strerror(5));
	printf("%s\n", strerror(6));
	printf("%s\n", strerror(7));
	printf("%s\n", strerror(8));
	printf("%s\n", strerror(9));
	printf("\n");

	FILE* P = fopen("test.txt", "r");

	if (P == NULL)
	{
		printf("%s\n", strerror(errno));
		perror("fopen");//作用跟strerror相同
		return 1;
	}
	return 0;
}

 Character classification function

function 
Returns true if its parameters meet the following conditions
iscntrl any control character
isspace
Blank character: empty case' ' ,换页 '\f' ,换行 '\v& #39;some vertical system notation'\t', System notation'\r', round '\n'
self digit Hexadecimal numbers, including all decimal digits, lowercase lettersa~f, uppercase lettersA~F
even
Decimal numbers 0~9
islower
Lowercase letters a~z
isupper Large character motherA~Z
isalpha
character a~z or A~Z
the ice hall
Letters or numbers, a~z,A~Z,0~9
ispunct
Punctuation marks, any graphic characters that are not numbers or letters (printable)
isgraph
any graphic character

Character conversion:

int tolower (int c);//Convert to lowercase
int toupper ( int c );//Convert to uppercase

Use of character segmentation functions and character conversion

#include<stdio.h>
#include<ctype.h>

void* de_tolower(char* arr)
{
	char* re = arr;
	while (*arr)
	{
		if (isupper(*arr))//判断是否为大写字母,是返回真,不是++检查下一个
		{
			*arr = tolower(*arr);//转换成小写字母
		}
		arr++;
	}
	return re;
}

int main()
{
	char arr[] = { "Hello WORLD" };
	char*re = (char*)de_tolower(arr);
	printf("%s\n", re);
	return 0;
}

 

If you have any questions or opinions, please leave a message in the comment area.

Guess you like

Origin blog.csdn.net/2202_75625589/article/details/128647086