实现常用字符串函数的功能

在现在的Linux版本下,支持许多的API函数,平时我们用得是挺舒服的,但是我们不光要用API函数,更重要的是,我们要知道这些API函数是怎样工作,执行的。我就用字符串的几个重要的函数来举例

一:字符串比较函数strcmp();

/*****************************************************
copyright (C), 2016-2017, Lighting Studio. Co.,     Ltd. 
Authorer  ChenYangYang     erey_Jobs    Version:0.1    Date: 2016/11/14
Description 自己实现字符串的比较函数
Funcion List: 
*****************************************************/

#include <stdio.h>

#define MAX_SIZE 1024

int my_strcmp(char *dest, char *src)
{
    
    char *temp = dest;
    while(*src != '\0')
    {
        if(*temp > *src)
        {
            return 1;
        }
        else if(*temp < *src)
        {
            return -1;
        }
        temp++;
        src++;
    }

    if(*src == '\0' && *temp == '\0')
    {
        return 0;
    }

    if(*src == '\0' && *temp != '\0')
    {
        return 1;
    }

    if(*temp == '\0' && *src != '\0')
    {
        return  -1;
    }
}

int main()
{
    char src[MAX_SIZE] = "hello world a";
    char dest[MAX_SIZE] = "hello world ";

    int result = my_strcmp(dest,src);

    printf("result = %d\n",result);

    return 0;
}



二:字符串连接函数strcat();

/*****************************************************
copyright (C), 2016-2017, Lighting Studio. Co.,     Ltd. 
Authorer ChenYangYang     erey_Jobs    Version:0.1    Date:2016/11/14 
Description 字符串连接函数
Funcion List: 
*****************************************************/

#include <stdio.h>

#define MAX_SIZE 1024

char *my_strcat(char *dest, char *src)
{
    char *temp = dest;
    while( *temp != '\0')
    {
        *temp++;
    }
    while(*src != '\0')
    {
        *temp = *src;
        *temp++;
        *src++;
    }
    *temp = '\0';
    return dest;
}

int main()
{
    char src[MAX_SIZE] = "hi  where are yo doing";
    char dest[MAX_SIZE] = "how are you";

    char *temp = my_strcat(dest,src);

    printf("temp = %s\n",temp);
    printf("dest = %s\n",dest);


    return 0;
}


三:字符串长度strlen();

/*****************************************************
copyright (C), 2016-2017, Lighting Studio. Co.,     Ltd. 
Authorer ChenYangYang erey_Jobs    Version:0.1    Date:2016/11/14 
Description 自己实现字符串长度函数
Funcion List: 
*****************************************************/

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1024

int my_strlen(char * src)
{
    int len = 0;
    while( * src != '\0')
    {
        *src++;
        len++;
    }
    return len;
}

int main()
{
    char src[MAX_SIZE] = "hello world";
    int len;
    len = my_strlen(src);

    printf("the len = %d\n",len);

    return 0;
}


四:字符串拷贝函数strcpy();

/*****************************************************
copyright (C), 2016-2017, Lighting Studio. Co.,     Ltd. 
Authorer ChenYangYang    Version:0.1    Date:2016/11/14 
Description 自己实现字符串拷贝函数
Funcion List: 
*****************************************************/

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 
char *my_strcpy(char *dest, char *src)
{
    char *temp = dest;
    while(*src != '\0')
    {
        *temp = *src;
        *temp++;
        *src++;
    }
    *temp = '\0';
    return dest;
}

int main()
{
    char src[MAX_SIZE] = "welcome to china";
    char dest[MAX_SIZE] = "hello world";

    char *temp = my_strcpy(dest,src);

    printf("temp = %s\n",temp);
    printf("dest = %s\n",dest);

    return 0;
}


阅读更多
版权声明:允许转载,标明出处就可以了 https://blog.csdn.net/baidu_35534327/article/details/53165259
文章标签: 函数 c语言 api
个人分类: c语言
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭