[C Language] Use of strcmp, strstr, strerror, memcpy functions

Article directory

  • 1. strcmp function
    • 1. Function description
      • 2. Function simulation implementation
  • 2. strstr function
    • 1. Function description
      • 2. Function simulation implementation
  • 3. strerror function
    • 1. The role of function
      • 2. Example usage
  • 4. memcpy function
    • 1. Function description
      • 2. Function simulation implementation

One, strcmp function

1, function description

int strcmp ( const char * str1, const char * str2 );

The above is cplusplus.com’s statement on the strcmp function. It can be seen that the strcmp function is a function with two const char* type parameters and a return value of int type.
strcmp() is a C\C++ standard library function. When using it, the header file string.h must be included. It is used to compare two strings for equality. It returns an integer value by comparing the contents of two strings. If you want to limit the number of characters compared, you need to use strncmp() which is not explained here. Here are the results it might return:

Return value Description
<0 str1
0 str1==str2
>0 str1>str2

where str1 and str2 are pointers to the two strings to be compared. The function compares the two strings character by character until an unequal character is encountered or the end-of-string character \0’ is encountered.

2, Function simulation implementation

Simulate the implementation of strcmp() through the following code

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
int my_strcmp(const char* str1,const char* str2)
{<!-- -->
    assert(str1 & amp; & amp; str2); //Assert, if str1 or str2 points to '\0', terminate the program directly
    while (*str1 & amp; & amp;*str1==*str2)
    {<!-- -->
        str1 + + ;
        str2++;
    }
    int ret = *str1 - *str2; //Create temporary variables for recording data
    if (ret)
        return 1; //arr1 > arr2
    else if (ret == 0)
        return 0; //arr1 == arr2
    else
        return -1; //arr1 < arr2
}
int main()
{<!-- -->
    char arr1[] = "abcdef";
    char arr2[] = "abcdef";
    printf("%d\
", my_strcmp(arr1,arr2));
    return 0;
}

The output is as follows:

Second, strstr function

1, function description

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

in:
str1 is the string to be found.
str2 is a substring.

The above is cplusplus.com’s declaration of the strstr function. It can be seen that the strstr function is a function with two char* type parameters and a return value of char* type.
strstr() is a C\C++ standard library function. When using it, you must include the header file string.h. It is usually used to search for specific substrings in text. , used for tasks such as finding keywords, validating input, and splitting strings. It is a very common and useful string processing tool. When using, also include the header file string.h. What it does is find another substring within a string. Returns the position of the first occurrence of the substring (a pointer to the first character of the substring). If the substring is not found, NULL is returned.

2, Function simulation implementation

Simulate the implementation of strstr() through the following code

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
const char* my_strstr(const char* str1, const char* str2)
{<!-- -->
    assert(str1 & amp; & amp; str2); //Assert, if str1 or str2 points to '\0', terminate the program directly
    const char* p = str1; //Create a temporary variable p to record the starting position of the matching string that may be found;
    const char* s1 = NULL;
    const char* s2 = NULL;
    if (*str2 == '\0') //If the substring is empty, return str1;
        return str1;
    while (*p) {<!-- -->
        s1 = p;
        s2 = str2;
        while (*s1 == *s2 & amp; & amp; s1 & amp; & amp; s2) {<!-- -->
            s1 + + ;
            s2++;
        }
        if (*s2 == '\0')
            return p;
        p + + ;
    }
    return NULL;
}
int main()
{<!-- -->
    char arr1[] = "abcdef";
    char arr2[] = "cde";
    printf("%s\
", my_strstr(arr1, arr2));
    return 0;
}

The returned p points to the address of the third element of arr1, so the output is as follows:

Three, strerror function

1, Function

char *strerror (int errnum);

in:
errnum is an integer representing the error code.

The above is cplusplus.com’s declaration of the strerror() function. It can be seen that the strerror function is a function with an int type parameter and a return value of char* type.
strerror() is a C\C++ standard library function. When using it, the header file string.h must be included. Function is used to convert an error code (usually an error code returned by a standard library or operating system function) into a corresponding error message string. Its function is to return the starting address of a corresponding error message string based on the given error code.
Here are some error codes:

2, example usage

#include <stdio.h>
#include <string.h>
#include <errno.h>
int main() {<!-- -->
    int err_code = ENOENT; // Indicates there is no error in the file
    char *error_message = strerror(err_code);
    printf("The error message corresponding to error code %d is: %s\
", err_code, error_message);
    return 0;
}

In the above example, the strerror function is used to convert the error code ENOENT (indicating that the file does not exist) into the corresponding error message string. The output displays the error code and corresponding error message.
The output is as follows:

Four, memcpy function

1, function description

void * memcpy ( void * destination, const void * source, size_t num );

The above is the declaration of the memcpy function by cplusplus.com. It can be seen that the memcpy function has two void* type parameters and one size_t type parameter, and the return value is a void* type function.
in:
destination is a pointer to the destination address.
source is a pointer to the source address.
num is the number of bytes to copy.

memcpy() is also a function in the C/C++ standard library. When using it, the header file string.h must be included. Its function is to copy the contents of the memory block from the source address to the target address. Then returns a pointer pointing to the target address (that is, the starting address of the copied data).
Compared with another function in the standard library strcpy, the former has a much wider scope of application. It is a common method of memory copy operation and can be used to copy memory blocks of any data type. Whether it is characters, integers, floating point numbers or custom structures, etc. And strcpy() is usually only used to copy C strings

2, Function simulation implementation

Simulate the implementation of memcpy() through the following code

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
void* my_memcpy(void* dest, const void* src, size_t num)
{<!-- -->
    assert(dest & amp; & amp; src); //Assert, if str1 or str2 points to '\0', terminate the program directly
    void* ret = dest; //Create a pointer to the initial address of dest
    for (size_t i = 0; i < num; i + + ) {<!-- -->
        *(char*)dest = *(char*)src;
        //Because we don’t know the type of the pointer, we convert it to char* type and then copy it.
        ((char*)dest) + + ;
        ((char*)src) + + ;
    }
    return ret;
}
int main()
{<!-- -->
    int arr1[6] = {<!-- -->1,2,3,4,5,6};
    int arr2[] = {<!-- -->6,5,4,3};
    my_memcpy(arr1, arr2, 12); //It is the number of bytes to be passed. Three integers are copied here, so 3X4=12
    for (int i = 0; i < 6; i + + )
        printf("%d ", arr1[i]);
    return 0;
}

The result is as follows:

One thing to note when using this function is that the memcpy function is suitable for copying data between non-overlapping memories. If there is overlap between memories and you want to copy, you can use the memmove function, here No explanation.