C language exercise 48: Summary of character functions and string functions

Summary of character functions and string functions

character function

1. Character classification function

There is a series of functions in C language that are dedicated to character classification, that is, what type of character a character belongs to. The use of these functions requires a header file called ctype.h

2. Character conversion function 

 String functions

.Usage of strlen

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

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

• Note that the return value of the function is size_t, which is unsigned (error-prone)

. The role of strlen

The strlen function is a string processing function in C language. Its function is to calculate the length of a string, that is, the number of characters in the string (excluding the terminating null character '\0'). Its function prototype is:

size_t strlen(const char *str);

Among them, str is a pointer to a character array (string). The function checks characters one by one starting from the str pointer position until it encounters the null character '\0' at the end of the string, and then returns the number of characters.

Use of .strcpy

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

• '\0' in the source string will be copied to the target space.

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

• The target space must be mutable.

The role of .strcpy

The strcpy function is a string processing function in C language, used to copy one string to another string. Its function is to copy the contents of the source string to the target string, including the null character '\0' at the end of the string. The prototype of the strcpy function is as follows:

char *strcpy(char *dest, const char *src);

Among them, dest is the pointer of the target string, and src is the pointer of the source string. After the function has finished executing, the destination string will contain the same content as the source string.

It should be noted that when using the strcpy function, you should ensure that the target string has enough space to store the content of the source string to avoid overflow errors. Additionally, the source string must be terminated with the null character '\0', otherwise undefined behavior will result.

Use of .strcat

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

• There must also be \0 in the target string, otherwise there is no way to know where to start appending.

• The target space must be large enough to accommodate the contents of the source string.

• The target space must be modifiable.

The role of .strcat

strcat is a C string manipulation function used to append one string to the end of another string. Its function is to copy the contents of the source string to the end of the target string and add a null character ('\0') to the end of the target string to mark the end of the string.

The function prototype is as follows:

 
 

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

Among them, destinationis the target string and sourceis the source string. The function returns a pointer to the target string.

When using the strcat function, you need to ensure that the target string has enough space to accommodate the contents of the source string, otherwise a buffer overflow error may occur.

Here is an example using the strcat function:

#include <stdio.h>
 #include <string.h> 
int main() {
 char destination[30] = "Hello"; 
const char *source = " World!"; 
strcat(destination, source); 
printf("%s\n", destination); 
return 0; 
}

The running result is:

Helo World!

In the above example, the source string "World!" is appended to the end of the target string "Hello", resulting in the final string "Hello World!". Note that when using the strcat function, you need to ensure that the target string has enough space to accommodate the contents of the source string.

.Usage of strcmp

◦ If the first string is greater than the second string, a number greater than 0 is returned.

◦ If the first string is equal to the second string, 0 is returned

◦ If the first string is less than the second string, a number less than 0 is returned.

Usage of strncpy function

• Copy num characters from the source string to the destination 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, up to num.

 .Usage of strncat function

The strncat function is used to append part of a string to the end of another string.

It accepts three parameters: target string, source string, and the maximum number of characters to append.

It copies characters from the source string to the end of the target string until the specified maximum number of characters is reached or the source string's terminator '\0' is encountered.

The function of this function is to concatenate part of the source string to the target string without overwriting the original content of the target string.

Usage of strncmp function

 Compare the first num characters of str1 and str2. If they are equal, continue to compare, up to num characters. If they are found to be different in advance, end early. The string with the larger character is larger than the other one. indivual. If num characters are equal, 0 is returned if they are equal.

Use of strstr

strstr is a function that searches for a substring in a string. It can be used to check if a string contains another string and return the position of the first occurrence. Its prototype is as follows:

char* strstr(const char* str1, const char* str2); 
where str1 is the string to be searched and str2 is the substring to be found. When a substring is found, the function returns the address pointing to the first occurrence of the substring; if not found, it returns NULL.

For example, we can use the strstr function to check whether a string contains a certain keyword. If the returned pointer is not NULL, the keyword exists in the original string.

.Usage of strtok function

• The sep parameter points to a string that defines the set of characters used as separators.

• The first parameter specifies a string that contains 0 or more tokens separated by one or more delimiters in the sep string.

• The strtok function finds the next token in str, ends it with \0, and returns a pointer to this token. (Note: The strtok function will change the manipulated string, so the string split using the strtok function is generally a temporary copy and can be modified.)

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

• The first parameter of the strtok function is NULL, and the function will search for the next mark starting from the saved position in the same string.

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

Usage of strerror function

The strerror function can return the string address of the error message corresponding to the error code in the parameter part. Some error codes are specified in different systems and the implementation of the C language standard library. They are generally described in the header file errno.h. A C language program will use one when it starts. The comprehensive variable errno records the current error code of the program. However, when the program starts, errno is 0, indicating that there is no error. When we use a function in the standard library and an error occurs, it will say The corresponding error code is stored in errno, and the number of an error code is an integer, which is difficult to understand what it means, so each error code has a corresponding error message. The strerror function can return the address of the error message string corresponding to the error.

 

Guess you like

Origin blog.csdn.net/2301_77479435/article/details/132790513