2019 1 21 付铖

/*strlen strcpy strcmp strcat 查看功能是什么,并自定义函数,与之功能一至*/
#include<stdio.h>

int strlen_long(char * ch);
void strcpy_cpy(char * c,char * ch);
int strcmp_compare(char * ch ,char * c);
int strcat_link(char * sz,char * ch,const char * c);

int main(void)
{
    int s,compare;
    char c[10];
    char ch[] ="hello";
    char sh[] = "helo";
    char * p;
    int x;
    x = sizeof(ch)/sizeof(*ch) + sizeof(sh)/sizeof(*sh) - 1;
    char  sz[x];

    /*strcmp*/
    compare = strcmp_compare(ch,sh);
    printf("%d\n",compare);

    /*strcat*/
    strcat_link(sz,ch,sh);
    printf("%s\n",sz);


    /*strcpy*/
    strcpy_cpy(c,ch);
    printf("%s\n",c);

    /*strlen*/
    s = strlen_long(ch);
    printf("%d\n",s);

    return 0;
}
/*strlen函数  用来返回字符串的长度 ,不包括‘\0’*/
int strlen_long(char * ch)
{
    int i = 0;
    while(* ch != '\0')
    {
        i++;
        ch++;
    }
    return i;
    
}
/* strcpy 函数用来把一个数组的字符串复制到另一个数值中*/
void strcpy_cpy(char* c,char * ch)
{
    int i = 0;
    do
    {
        *(c + i) = *(ch + i);
        i++;
    }while(*(ch + i) != '\0');
}
/*strcmp 函数用来比较字符大小,从左到右最开始不同的字符,asill码值的差值 */
int strcmp_compare(char * ch ,char * c)
{
    int i = 0;
    while(*(ch + i) == *(c + i))
    {
        if(*(ch + i) != '\0')
            i++;
        else
            break;
    }
    return *(ch+i) - *(c+i);

}
/*strcat函数用来拼接两个字符串*/
int strcat_link(char * sz,char * ch,const char * c)
{
    int i,j = 0;

    for(i = 0; * (ch + i) != '\0';i++)
    {
        *(sz + i) = *(ch + i);
    }
    do
    {
        *(sz + i) = *(c + j);
        i++;
        j++;
    }while(*(c + j) != '\0');



}

猜你喜欢

转载自www.cnblogs.com/fc724/p/10300722.html
今日推荐