Detailed explanation of character functions and string functions

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.


Preface

世上有两种耀眼的光芒,一种是正在升起的太阳,一种是正在努力学习编程的你!一个爱学编程的人。各位看官,我衷心的希望这篇博客能对你们有所帮助,同时也希望各位看官能对我的文章给与点评,希望我们能够携手共同促进进步,在编程的道路上越走越远!

想回顾上一节的请点击这里深入理解指针5


提示:以下是本篇文章正文内容,下面案例可供参考

1. Character classification function

There are a series of functions in the 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 ctype.h

The usage of these functions is very similar. We will explain one function, and the others are very similar:

int islower ( int c );

islower can determine whether the c in the parameter part is a lowercase letter.

Use the return value to indicate whether it is a lowercase letter. If it is a lowercase letter, it returns a non-zero integer. If it is not a lowercase letter, it returns 0.

Code demo:

Write a code that converts lowercase characters in a string to uppercase, leaving other characters unchanged.

2. Character conversion function

C language provides 2 character conversion functions:

int tolower ( int c ); //将参数传进去的大写字⺟转小写
int toupper ( int c ); //将参数传进去的小写字⺟转大写

In the above code, we convert lowercase to uppercase, which is the effect of -32. With the conversion function, we can directly use the above two functions.

#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 (islower(c)) 
   c = toupper(c);
   putchar(c);
   i++;
 }
 return 0;
}

3. Usage and simulation implementation of strlen

size_t strlen ( const char * str );

• The string ends with '\0', and the strlen function returns the number of characters that appear before '\0' in the string (excluding '\0& #39; ).

• 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)

• Learn the simulation implementation of strlen function

 3.1Usage of strlen:

Code demo:

3.2Simulation implementation of strlen:

method one:

Method Two:

Method three:

4. Use and simulation implementation of strcpy

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

strcpy
Function: copy string
Notes:
1: The source string must contain \0, and \0 must also be copied to the target space
2: The programmer must ensure that the target space is large enough to accommodate the copied data
3: Ensure that the target space must be modifiable

4.1Usage of strcpy

4.2 Simulation implementation of strcpy

 5. The use and simulation implementation of strcat

strcat--string append
Notes:
1: There is \0 in the target space (where to start appending), source character The string contains \0 (when appending ends, including \0)
2: The target space must be large enough
3: The target space must be modifiable (characters Arrays can be modified, but constant strings cannot be modified)

5.1 Use of strcat 

 5.2 Simulation implementation of strcat

When appending a string to itself, use the strncat() function

6. Usage and simulation implementation of strcmp

strcmp--string compareString comparison
Compare the size relationship between two strings
Compare the characters at corresponding positions in the two strings. Compare according to dictionary order (that is, the ASCII code value of the letter)

6.1Usage of strcmp

6.2 Simulation implementation of strcmp

7. Comparison of strcpy and strncpy (including other two pairs)

strcpy--copy string
strcat--append string
strcmp--compare string
Unlimited length string functions

strnapy--copy string
strncat--append string
strncmp--compare string
Limited-length string functions

Code demo:

8. Use of strtok function

char * strtok ( char * str, const char * sep);

• The sep parameter points to 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, terminates 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 token starting from the saved position in the same string.

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

Let's take a look at the code demonstration:

There is also an improved version:

9. Usage and simulation implementation of strstr

char * strstr ( const char *, const char * );

The strstr() function finds a substring in a string

Strstr() function function:
1: Return the position where str2 appears in str1
2: If str2 does not appear in str1, Just return NULL

9.1Usage of strstr

9.2 Simulation implementation of strstr function

10. Use of strerror function

 char * strerror ( int errnum );

strerror() function: returns the starting address of the error message string corresponding to an error code

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, usually placed in errno.h As stated in the header file, when a C language program starts, it will use a comprehensive variable errno to record the current error code of the program, but the program starts When errno is 0, it means there is no error. When we use a function in the standard library and an error occurs, the corresponding error code will be stored in errno. It is difficult for an error code number to be an integer. 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.

Code demo:


Summarize

Okay, this blog ends here. If you have a better point of view, please leave a message in time. I will watch it carefully and learn from it.
If you don’t accumulate steps, you can’t reach a thousand miles; if you don’t accumulate small streams, you can’t become a river or sea.

Guess you like

Origin blog.csdn.net/2301_79585944/article/details/134272148