首页 > 其他 > 详细

2019 1 21 付铖

时间:2019-01-21 20:40:50      阅读:170      评论:0      收藏:0      [点我收藏+]
/*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);



}

 

2019 1 21 付铖

原文:https://www.cnblogs.com/fc724/p/10300722.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!