Play with string functions and character functions - [C language]

In the learning of C language, we often encounter strings, and there are countless ways to deal with them, but we don't have a good way to deal with them. Strings can only be used in character arrays or constant strings through custom functions. deal with. Now I will lead you to learn the functions in the C language function library that deal with strings.

All these string processing functions are used in #include<string.h> and #include<type.h>! Next, let's get to know these functions in detail! ! !

Table of contents

Function introduction in string.h

strlen function

strcpy functionEdit

strcat function

strcmp function

strstr function

 strtok function

 More rigorous library functions

strncpy function

strncat function

strncmp function 

summary 

Function introduction in ctype.h 


Function introduction in string.h

strlen function

The strlen function is to get the size of the string function (function)

1. The return value type is size_t (unsigned integer)

2. The parameter is to receive a pointer of unmodifiable char* type

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

4. The return value of the strlen function is to return the length of the string. When the pointer points to '\0', the counting ends, so the length of the string does not include '\0'. 

The following program uses the strlen function:

#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;
}

Code analysis shows that the string length of str1 is 6, and the string length of str2 is 3. 3-6=-3 should output str1>str2, but the actual result is: why  ? Because the type returned by the strlen function is an unsigned integer, the default result of subtracting two numbers is also an unsigned integer. The complement of -3 in binary storage is very large, but the computer thinks that the complement of -3 is its The original code, so converted to decimal will be a very large positive number, so the output result is wrong.

If we replace the condition in the if condition judgment with (strlen(str2)>strlen(str1)); the result will be correct!

Let's create a custom function to simulate the implementation of the strlen function.

//模拟strlen函数

size_t my_strlen(char* arr)
{
	int count = 0;
	while (*arr != '\0')
	{
		count++;
		arr++;
	}
	return count;
}

int main(void)
{
	char arr[100];
	gets(arr);
	printf("%u\n", my_strlen(arr));

	return 0;
}

 We simulate the my_strlen function by imitating the parameters, return value and principle of the strlen function. Specifically, pass in the first element of the character array we require, create a temporary variable count to count the number of the incoming array, use the while loop to judge whether the pointer points to '\0', if not point to count++, arr++ to count and point to Then move until it reaches '\0' to jump out of the loop, return the number of count to realize the simulation of the strlen function.


strcpy function

The srtcpy function copies the string pointed to by the source into the pointed array;

1. The return value is a char* pointer, pointing to the address of the first element of the copied string.

2. The parameters are two pointers of char* type, the latter one is the copied string, so it cannot be modified, and the former one is the copied string.

3. The source string must end with '\0'.

4. The target space must be large enough to ensure that the source string can be stored.

5. The target space must be variable.

6. This function will copy '\0' in the source string to the target space.

The following is the use of the strcpy function:

int main(void)
{
	char arr1[100];
	char arr2[100] = "hello world";
	char* p = strcpy(arr1, arr2);
	printf("%s\n", p);

	return 0;
}

Copy the string in arr2 to arr1. 

 The following is a simulation of the strcpy function:

模拟strcpy函数
char* my_strcpy(char* p1, const char* p2)
{
	assert(p1 && p2);
	char* ret = p1;
	while (*p1++ = *p2++)
	{
		;
	}
	return ret;
}


int main(void)
{
	char arr1[100];
	char arr2[100];
	gets(arr2);
	char* p = my_strcpy(arr1, arr2);
	printf("%s\n", p);
	return 0;
}

Our purpose is to copy the content in arr2 to arr1, create a custom function my_strpy function imitating the function template of the strcpy function, and use the assert function to assert to prevent the incoming pointer from being a null pointer. Create a character pointer to mark the address of the first element of the target array as the return value. Use the while loop to judge the condition. If p2 is not '\0', then perform *p1=*p2 assignment. After the assignment is successful, both pointers move backward one bit until p2 is '\0' and the cycle ends to complete the copy ( The operation can be simplified to the judgment condition of the while statement), and finally the address of the target array can be returned.

The result is as follows:


strcat function

The strcat function is a function of concatenating strings, concatenating a known string to a target string.

1. The return value is the address of the first element of the target string

2. The two parameters are character pointers of char* type. The latter pointer is the first address of the known string element and cannot be modified, so it is modified with const. The front pointer is the first address of the target string.

3. The source string must end with '\0'.

4. The destination space must be large enough to accommodate the contents of the source string.

5. The target space must be modifiable.

 The following is the use of the strcat function:

int main(void)
{
	char arr1[20] = "hello ";
	char arr2[20] = "world";
	char* p = strcat(arr1, arr2);
	printf("%s\n", p);

	return 0;
}

The principle of the strcat function: find the target string '\0' to overwrite, add the known string to the end of the target string until the end of the known string '\0', and return the first element pointer of the target string .

 Finally, the analog implementation of the strcat function:

char* my_strcat(char* p1, const char* p2)
{
	assert(p1 && p2);
	char* ret = p1;
	while (*p1 != '\0')
		p1++;
	while (*p1++ = *p2++)
	{
		;
	}
	return ret;
}

int main(void)
{
	char* arr1[100];
	char* arr2[100];
	gets(arr1);
	gets(arr2);
	char* p = my_strcat(arr1, arr2);
	printf("%s\n", p);

	return 0;
}

 The goal is to put the known string at the end of the target string, so when we receive two pointers, we first mark the first element of the target function to prepare for the final return value, and then we use the while loop to point the pointer of the target string to Go to the end '\0', and then the operation is the same as the principle of the strcpy function, copy, and finally return the first element of the target function to complete.

The following is the result of running the code:


strcmp function

The strcmp function is a function that compares the size of two strings.

1. The return value is a number of type int

If the first string is greater than the second string, return a number greater than 0

If the first string is equal to the second string, return 0

The first string is less than the second string, returns a number less than 0 

2. The parameters are two strings for comparison. In order to protect the two strings from being modified unintentionally, they are all modified with const.

3. This function starts comparing the first character of each string. If they are equal to each other, continue with the following pairs until the characters differ or the terminating null character is reached.

The following is the use of the strcmp function:

int main(void)
{
	char arr1[100] = "hello";
	char arr2[100] = "hell";
	printf("%d\n", strcmp(arr1, arr2));

	return 0;
}

The two strings of hello and hell, the front hell is all equal, only the last o is compared with '\0', according to the ascaii code value o is larger than '\0', so the arr1 string is larger than the arr2 string, return 1. 

Finally, the strcmd function is simulated and implemented:

//模拟实现strcmp函数

int my_strcmp(const char* p1, const char* p2)
{
	assert(p1 && p2);
	while (*p1 == *p2)
	{
		p1++;
		p2++;
	}
	if (*p1 - *p2 > 0)
		return 1;
	else if (*p1 - *p2 < 0)
		return -1;
	else
		return 0;
}


int main(void)
{
	char arr1[100];
	char arr2[100];
	gets(arr1);
	gets(arr2);
	printf("%d\n", my_strcmp(arr1, arr2));
	return 0;
}

Pass the two strings into the function, use the while loop to compare (*p1 == *p2), if they are equal, move the two pointers back one bit to continue the comparison, if they are the same, return 0, if *p1>* p2 returns 1, and *p1<*p2 returns -1.

The following are the results of the operation:


strstr function

The function of the strstr function is to find a substring. The substring is str2 and the target string is str1. 

1. The return value is a pointer of char* type, and returns   a pointer to str1 that appears for the first time in  str2 . If str2 is not   a part of str1 , a null pointer is returned.

2. The parameter type is two character pointers, which point to the target string and substring respectively. Because they cannot be modified, const is added for modification.

3. The matching process does not include the terminating null character, but it stops there.

Here is our use of the strstr function:

int main(void)

{
	char arr1[100] = "hello world";
	char arr2[100] = "lo";
	char* p = strstr(arr1, arr2);
	printf("%s\n", p);

	return 0;
}

The above is the search for the substring lo in the target string hello world. When it is found, it will return the string starting from lo and start printing. The result should be: lo world. Then let's see if the result is the same as we imagined?

 The running results are the same as we imagined.

Below we simulate the implementation of the strstr function:

//模拟strstr函数

char* my_strstr(const char* p1, const char* p2)
{
	char* ret = p1;
	char* w = p2;
	char* flag = NULL;
	while (*ret != '\0')
	{
		if (*ret == *w)
		{
			flag = ret;
			while (*w != '\0')
			{
				if (*ret != *w)
				{
					w = p2;
					ret = flag;
					flag = NULL;
					break;
				}
				w++;
				ret++;
			}
		}
		ret++;
	}

	return flag;
}

int main(void)
{
	char* arr1[100];
	char* arr2[100];
	gets(arr1);
	gets(arr2);
	char* p = my_strstr(arr1, arr2);
	printf("%s\n", p);

	return 0;
}

This function is simulated like the parameters and return value of the strstr function, because we need to find the position of the sub-function corresponding to the target function, so we have to create a temporary pointer of type char* for marking, and then use the while loop to find the target function The same as the first content of the sub-function, use the pointer just created to mark the first element, and then nest a loop to compare all the content, if all are the same, return the address marked by the pointer just now, if not, return the sub-function pointer At the beginning, the target function continues to compare to the next position until the target is found, and if the content of the sub-function is not found, a null pointer is returned.

The result of the operation is as follows:


 strtok function

The strtok function is a string splitting function, which splits a whole string according to the given splitting symbol.

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

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

3. The strtok function finds the next mark in str, ends it with \0, and returns a pointer to this mark. (Note: The strtok function will change the string being manipulated, so the strings split using the strtok function are generally temporary copied content and can be modified.)

4. The first parameter of the strtok function is not NULL, the function will find the first mark in str, and the strtok function will save its position in the string.

5. The first parameter of the strtok function is NULL, the function will start at the saved position in the same string, and search for the next mark.

6. If there are no more tokens in the string, a NULL pointer is returned.
 

When the strtok function is called for the first time, the found character will be used as a mark. If we pass a null pointer to the first parameter when we call this function for the second time, the strtok function will continue in the string passed in last time. Find the target character and segment it. Therefore, when using this function, it will be used in combination with a for loop.

Specific usage method:

int main(void)
{
	char arr[] = "[email protected]";
	char sep[] = "@.";
	char copy[50];
	strcpy(copy, arr);
	char* ret = NULL;
	for (ret = strtok(copy, sep); ret != NULL; ret = strtok(NULL, sep))
	{
		printf("%s\n", ret);
	}

	return 0;
}

 The result of the operation is as follows:


 More rigorous library functions

Above we learned strcpy, strcat, strcmp functions, they all pay more attention to '\0', but they don't pay attention to the additional characters, we call these functions string functions with unlimited length. Next we accept some length-restricted string functions.

They are the strncpy, strncat, and strnmp functions, which are n more than the functions learned before. What is the difference between the function prototype and the previous one?

 

They all have one more parameter size_t num than the previous function prototype, and this num limits the required characters. The function is exactly the same as the previous function.

So why do you say they are more rigorous?

strncpy function

int main(void)
{
	char arr1[20] = "abcdef";
	char arr2[] = "xxxxxxxxxxxxxxxx";
	strncpy(arr1, arr2, 3);

	return 0;
}

 Although the string in arr2 is very long, we only copy three characters into arr1.

What if there are only three characters in the string in our arr2, but we write 5 as the third parameter in strncpy?

 The remaining ones will be replaced with '\0' until 5 copies are satisfied.

But the strncat function is different! ! !

strncat function

int main(void)
{
	char arr1[20] = "abcdef'\0'yyyyy";
	char arr2[] = "xxx";
	strncat(arr1, arr2, 5);

	return 0;
}

Because the principle of the strncat function is basically the same as that of the strcat function, it will stop and append a string when it encounters '\0'. If we give a normal string, it will be followed by '\0' and cannot see that there are not enough additional characters. changes, so we use this special string for debugging!

 

 From this debugging, we can see the difference between strncat and strncpy. The strncat function does not care whether there are enough characters, just add the known characters and finally add '\0'.

strncmp function 

The strncmp function has the same function as the strcmp function, that is, the number of comparisons is limited by size_t num, and the usage is also very simple. I will give a program:

int main(void)
{
	char arr1[100] = "abdefghigk";
	char arr2[100] = "abdfgiigk";
	printf("%d\n", strncmp(arr1, arr2, 2));
	printf("%d\n", strncmp(arr1, arr2, 4));

	return 0;
}


summary 

 In general, those with n are safer than those without n, because they limit the size, we will pay attention to a series of issues such as whether the array space can be stored when using it.

But under normal use, the two types of functions are the same, and one person can't stop writing bugs! ! !


Function introduction in ctype.h 

The functions in ctype.h are functions for processing characters, as long as we can remember these functions and use them together! ! !

The following are some important functions in ctype.h:

Character judgment function:

 function Returns true if its argument meets the following conditions
iscntrl any control characters
isspace Whitespace characters: space ' ', form feed '\f', line feed '\n', carriage return '\r', tab '\t' or vertical tab '\v'
even Decimal number 0~9
self digit Hexadecimal numbers, including all decimal numbers, lowercase letters a~f, uppercase letters A~F
islower lowercase letters a~z
isupper Capital letters A~Z
isalpha Letter a~z or A~Z
the ice hall Letter or number, a~z,A~Z,0~9
ispunct Punctuation, any graphic character that is not a number or letter (printable)
isgraph any graphic character
sprint Any printable character, including graphic characters and white space characters

Character conversion function:

int tolower ( int c );

int toupper ( int c ); to lowercase

Specific usage method: 

#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char str[]="Test String.\n";
  char c;
  while (str[i])
 {
    c=str[i];
    if (isupper(c))
        c=tolower(c);
    putchar (c);
    i++;
 }
  return 0;
}

Analysis: Convert lowercase characters in a string to uppercase.

 

 The above is my description of the basic string functions and character functions. If you have any shortcomings, you can leave a message in the comment area. Bloggers will learn patiently. Your support is my biggest motivation! ! !

 

Guess you like

Origin blog.csdn.net/m0_74755811/article/details/131698870