On the realization of string library function

#include<stdio.h>
#include<assert.h>
#include<ctype.h>

//strcpy(p, p1) copy string 
///strncpy(p, p1, n) copies the specified length string
char *My_strcpy1(char *des, const char *src, int len1, int len2)
{
    char *p = des;
    assert((len1 >= len2)&&(*src != NULL));
    while(*src != '\0')
    {
        *des = *src;
        des++;
        src++;
    }//while(*des++ = *src++)
    *des = '\0';
    return p;
}
//Copy
char *My_strcpy2(char *des, const char *src, int len1, int len2)
{
    assert((len1 >= len2)&&(*src != NULL));
    int i;
    for(i = 0; src[i] != '\0'; i++)
    {
        des[i] = src[i];
    }
    des[i] = '\0';
    return des;
}
//Copy
char *My_strncpy(char *des, const char *src, int len1, int len2, int n)
{
    assert((len1 >= len2)&&(*src != NULL));
    int i;
    for(i = 0; src[i] != '\0' && i < n; i++)
    {
        des[i] = src[i];
    }
    return des;
}
//Copy

//strlen(p) takes the string length
int My_strlen(const char *src)
{
    int temp = 0;
    while(*src != '\0')
    {
        temp++;
        src++;
    }
    return temp;
}
//length

//strcmp(p, p1) compare strings 
//strncmp(p, p1, n) compares a specified length string
int My_strcmp(const char *str1, const char *str2)
{
    assert((*str1 != NULL)&&(*str2 != NULL));
    int temp;
    while((temp = *str1 - *str2) == 0)
    {
        str1++;
        str2++;
    }
    return temp;
}
//compare
int My_strncmp(const char *str1, const char *str2, int n)
{
    assert((*str1 != NULL)&&(*str2 != NULL));
    int i;
    int temp;
    for(i = 0; str2[i] != '\0' && i < n; i++)
    {
        while((temp = *str1 - *str2) == 0)
        {
            str1++;
            str2++;
        }
    }
    return temp;
}
//compare

//strcat(p, p1) additional string 
//strncat(p, p1, n) appends the specified length string
char *My_strcat(char *des, const char *src)
{
    assert(src != NULL);
    char *p = des;
    while(*des != '\0')
    {
        des++;
    }
    while(*src != '\0')
    {
        *des++ = *src++;
    }
    *des = '\0';
    return p;
}
//String connection
char *My_strncat(char *des, const char *src, int n)
{
    assert(src != NULL);
    char *p = des;
    while(*des != '\0')
    {
        des++;
    }
    for(int i = 0; *src != '\0' && i < n; i++)
    {
        *des++ = *src++;
    }
    *des = '\0';
    return p;
}
//String connection

void Func(char *str1)
{
    assert(*str1 != NULL);
    int flag = 0;
    int word = 0;
    int count = 0;
    while(*str1 != '\0')
    {
        if(*str1 == ' ')
        {
            flag = 0;
        }
        if(isalpha(*str1))
        {
            count++;
            if(*str1 != ' '&& flag == 0)
            {
                flag = 1;
                word++;
            }
        }
        str1++;
    }
    printf("Number of characters  %d\n", count);
    printf("Word quantity  %d\n", word);
}
//Number of characters, number of words

char *My_itoa(int num, char *str)
{
    char *p = str;
    int i = 0;
    int j;
    char temp;
    int flag = 1;
    while(num != 0)
    {
        if(num < 0)
        {
            num = -num;
            flag = -1;
        }
        str[i] = num % 10 + '0';
        num = num / 10;
        i++;
    }
    if(flag < 0)
    {
        str[i++] = '-';
    }
    str[i] = '\0';
    i--;
    for(j = 0; j < i; j++, i--)
    {
        temp = str[j];
        str[j] = str[i];
        str[i] = temp;
    }
    return p;
}
//Integer to character
int My_atoi(const char *str)
{
    assert(*str != NULL);
    int flag = 1;
    //int i;
    int num = 0;
    while(isspace(*str) == ' ')
    {
        str++;
    }
    if(*str == '-')
    {
        flag = -1;
        str++;
    }
    else if(*str == '+')
    {
        str++;
    }
    while(*str != '\0' && isdigit(*str))
    {
        num = num * 10 + *str - '0';
        str++;
    }
    return num*flag;
}
//Character to integer

int main()
{
    char str[30];
    char *str4 = "1a2b3";
    char *str5 = "-123";
    char str1[30] = "ab cd ef fe  ";
    char *str3 = "   ab cd ef";
    char *str2 = "tulun";
    int len1 = sizeof(str1)/sizeof(str1[0]);
    int len2 = sizeof(str2)/sizeof(str2[0]);
    printf("My_strcpy1  %s\n", My_strcpy1(str1, str2, len1, len2));
    printf("My_strcpy2  %s\n", My_strcpy2(str1, str2, len1, len2));
    printf("My_strncpy  %s\n", My_strncpy(str1, str2, len1, len2, 3));
    printf("My_strlen  %d\n", My_strlen(str1));
    printf("My_strcmp %d\n", My_strcmp(str3, str2));
    printf("My_strncmp  %d\n", My_strncmp(str3, str2, 3));
    printf("My_strcat  %s\n", My_strcat(str1, str2));
    printf("My_strncat  %s\n", My_strncat(str1, str2, 2));
    printf("My_itoa  %s\n", My_itoa(-123, str));
    printf("My_atoi  %d\n", My_atoi(str4));
    printf("My_atoi  %d\n", My_atoi(str5));
    Func(str1);
    Func(str3);
    getchar();
    return 0;
}

1. Function of return value: chain expression

int len = strlen(strcpy(str1, str2));

2. About const: fixed nearest
const indicates that the content is unchanged before *;
const after * indicates that the pointer does not change;

const char *str1 = char const *str2 = "abc";//Constant string, pointing content cannot be modified
char *const str2 = "def";//const pointer
str1 = str2;

3. Assert function: assert, find the error accurately and quickly (under debug condition)

#include <assert.h>
void assert( int expression );

The function of assert is to evaluate the expression. If its value is false (that is, 0), it first prints an error message to stderr, and then stops the program by calling abort.

4,
A series of functions in c language, such as isalpha, isdigit, islower, isupper, etc

5. * (str + 1) and str[1] access efficiency:
(1)*(str + 1)
STR address - > take out the str storage data address - > access data
(2)str[1]
str address - > str + 1

Keywords: C

Added by Imtehbegginer on Tue, 31 Mar 2020 22:30:04 +0300