Simulate the str function

    Strings are an important data type, but C does not have an explicit string data type because strings are stored in character arrays as character constants. Next I'm going to simulate implementing library functions that deal with strings: strlen, syrcpy, strcat, strstr, strcmp, memcpy.
1. strlen returns an int type of data, which is a function to test the length of a string.
This is the function of simulating strlen by yourself. The
idea : the string has an end mark \0, as long as you traverse the string and know the end of \0, you can easily get the length of the string

#include <stdio.h>
#include <stdlib.h>
int my_strlen(const char* str)
{
    if (*str == '\0')
    {
        return 0;
    }
    else
    {
        int ret = (1 + my_strlen(str + 1));
        return r`
t;
    }
}int main()
{
    const char* string = "hello";
    printf("长度为:%d",my_strlen(string));
    system("pause")`

    return 0;
}

There are many ways to implement the strlen function, and here I only stick to a recursive method.
2. Simulate the implementation of strcpy to represent the string copy function, but I think it is better to use less, because the destination string must be given enough space, otherwise it will produce unpredictable results, strncpy is much easier to use than it. You can specify the size yourself.
Idea: Copy the src string to dst. Since the dst parameter is modified, it must be a character array or a pointer to an array of dynamically allocated memory, and string constants cannot be used. The new string is meant to end with \, so the remaining few characters of the old string will be effectively deleted

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char *my_strcpy(char* str1, const char *str2)
{
    assert(str1!=NULL);
    assert(str2 != NULL);
    char* ret = str1;
    while ((*str1++ = *str2++))
    {
        ;
    }
    return ret;
}
int main()
{
    char string1[]="hehehhehe" ;//strcmp的目的字符串必须给足够大的空间,否则由于空间不够大而产生不可预料的结果
    const char* string2 = "kitty";
    printf("%s\n", my_strcpy(string1, string2));
    system("pause");
    return 0;
}

3. The realization of strcat. strcat is to add a string to the back of another string. The idea is to find the end of the destination string and add a copy of the source string to this position.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char *my_strcat(char *dest, const char *src)
{
    assert(dest!=NULL);
    assert(src!=NULL);
    char* ret = dest;
    while (*dest)//遇到\0结束
    {
        dest++;
    }
    while ((*dest++ = *src++))
    {
        ;
    }
    return ret;
}
int main()
{
    char str1[12] = "hello";
    const char* str2 = "world";
    printf("%s\n", my_strcat(str1, str2));
    system("pause");
    return 0;
}

4. String comparison function.
Idea: Comparing two strings is to compare the characters of the two strings one by one, until the mismatch position is found. Since strcmp does not change any of the parameters, there is no disadvantage of overflowing the character array, but the parameters of strcmp must have an end marker at the end, otherwise strcmp may compare the content behind the parameters, and the result obtained in this way is meaningless.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int my_strcmp(const char* dst, const char* src)
{
    int ret = 0;
    while (!(ret = *(unsigned char*)dst - *(unsigned char*)src) && dst)
    {
        ++dst;
        ++src;
    }
    if (ret > 0)
        return 1;
    else if (ret < 0)
        return -1;
    return ret;
}
int main()
{
    char arr1[] = "abcdef";
    char arr2[] = "abbdcef";
    printf("%d", my_strcmp(arr1, arr2));
    system("pause");
    return 0;
}

5. Simulate the implementation of the memcpy function.
Since the string function will stop working when it encounters the first NUL byte, this requires that the string cannot contain any NUL bytes inside, but it is common for the string to contain NUL bytes inside, so we use memory operations to solve this question. memcpy can copy n bytes of the source string to the destination string, using this method to copy any type of value; the specific operations are as follows:

#include <stdio.h>
#include <stdlib.h>
void* my_memcpy(void* str1, const void* str2, size_t c)
{
    void* ret = str1;
    while (c--)
    {
        *(char*)str1 = *(char *)str2;
        str1 = (char*)str1 + 1;
        str2 = (char*)str2 + 1;
    }
    return ret;
}

int main()
{
    char arr1[] = "aaaaaaaaaa";
    char arr2[] = "bbbbbbbbbb";
    int count = 5;
    printf("%s\n", my_memcpy(arr1, arr2, count));
    system("pause");
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324891302&siteId=291194637