[c language 12] string function (strlen, strcmp, strcpy, strcat, strstr, strtok, strerror)

Please add a picture description

Library function (Library function) is a way to put functions in the library for others to use. .The method is to compile some commonly used functions into a file for different people to call. When calling, just add the file name where it is located with #include<>. It is usually placed in the lib file.
Generally, it refers to the functions provided by the compiler that can be called in the C source program. It can be divided into two categories, one is the library function stipulated by the C language standard, and the other is the compiler-specific library function.
Today I'm going to introduce string functions.

1. String functions

1.1strlen (find the length of the string)

The strlen function is usually used to calculate the length of a string. It should be noted that if there is no '\0' in the initialized string, the calculated length is a random value.
Code details:

int main()
{
    
    
	char arr[] = "hello world";
	//strlen函数的参数是字符串的首地址,
	//同时计算的长度需要保存在整型变量中;
	int len = strlen(arr);
	printf("%d", len);
	return 0;
}

operation result
insert image description here

1.2 strcmp (compare strings)

The strcmp function is usually used to compare strings, and compares the ASCII code values ​​​​of characters. The parameter of the string is
strcmp(str1 - the first string to be compared, str2 - the second string to be compared.) The
return value of this function is as follows:
if the return value is less than 0, it means that str1 is less than str2 .
If the return value is greater than 0, it means that str1 is greater than str2.
If the return value is equal to 0, it means that str1 is equal to str2.
The process of strcmp comparison is: when the first address points to
the code detailed explanation

int main()
{
    
    
	char arr[] = "hello world";
	char bit[] = "hello bit";
	int size = strcmp(arr, bit);//strlen的详细参数
	if (size < 0)//判断
	{
    
    
		printf("arr<bit\n");
	}
	else if(size == 0)
	{
    
    
		printf("arr=bit\n");

	}
	else
	{
    
    
		printf("arr>bit\n");
	}

	return 0;
}

Comparison result
insert image description here
Because the ASCII code value of the seventh character 'w' in arr is greater than the 'b' of bit;
so arr>bit;

1.3 strcpy (copy string)

The strcpy function is usually used to copy a string to another string. Note that the space of the copied string must be large enough, otherwise a numerical overflow error will occur.
Detailed code

int main()
{
    
    
	char arr[20];
	char *p = "hello csdn";
	strcpy(arr, p);//使用指针拷贝
	char str[] = "hello world";
	char arr2[20];
	strcpy(arr2, str);//使用字符串首地址拷贝
	printf("%s\n", arr);
	printf("%s\n", arr2);

	return 0;
}

We noticed that strings can be copied by using pointers. This is because the function required by strcmp only needs to be a pointer to the first character of the string. Running
result

insert image description here

1.4 strcat (append string)

The function of the strcat function is to append a string. The function of this function is similar to that of the strcpy function, but it is not the same. The author's current learning level cannot be interpreted in detail. When the learning is in place, be sure to fill in
the detailed code

int main()
{
    
    
	char Moon[100] = "今夜月色真美";
	char wind[] = " 风也温柔";
	strcat(Moon, wind);//把wind字符串中的内容追加到Moon的词尾后
	printf("%s", Moon);
	return 0;
}

operation result
insert image description here

1.5strstr (judgment substring)

The function of the strstr function is to determine whether a string contains the content of another string. When found, returns the position in the main string where the substring exists, or NULL if it does not exist. The text description may be a bit convoluted, we still try to explain it with code
Code Detailed Explanation

int main()
{
    
    
	char arr[] = "hello world i live you";
	char str[] = "world";
	char *ret = strstr(arr, str);//ret接受的是arr中str字符串的起始位置
	printf("%s", ret);
	return 0;
}

Running results
insert image description here
At the same time, we can use the characteristics returned by it to judge whether there are substrings in the main string.
For example,
insert image description here
C language is really a wonderful language. The more you learn, the more you can feel the wisdom of our ancestors.

1.6sttok (string split)

The role of the strtok function is: first define a collection of characters, which contains the characters you want to split. Still talking about the code
Detailed explanation of the code

int main()
{
    
    
	char mailbox[] = "Nancat@bi,sheng.com";//主串
	char arr[] = "@,.";//需要分割的符号集合体
	char* ret = NULL;
	for (ret = strtok(mailbox, arr); ret != NULL; ret = strtok(NULL, arr))
	{
    
    
		printf("%s\n", ret);
	}
	return 0;
}

Running Results
insert image description here
The main string was successfully split

1.7 strerror (return error code)

The strerror function usually judges errors. It is a relatively commonly used library function. It can clearly tell you what caused the error. It is very
easy to use;
the code is explained in detail

int main()
{
    
    
	FILE* p = fopen("red.exe", "r");
	if (p == NULL)
	{
    
    
		printf("%s", strerror(errno));
	}
	return 0;
}

operation result
insert image description here

Summarize

C language is endless, I am just an insignificant passer-by on the road of learning, if someone reads my blog, I hope you can give me more guidance, thank you for everyone who has read the blog and pointed out my shortcomings.

Guess you like

Origin blog.csdn.net/nanmiao666/article/details/131383967