[C语言]字符串函数的分析以及模拟实现

我们先来总结一下字符串函数有哪些?
我们把字符串函数分为两大类,一种是不安全的,一种是优化版本
strlen

strcat,strcmp,strcpy,strstr

strncat,strncmp,strncpy

我们为什么会有strncat,strncmp,strncpy
这些函数呢,原因是因为strcat,strcmp,strcpy这些函数的第一个参数的数组大小不能保证有足够大的空间容纳,如果第一个参数数组并没有开辟足够大的内存空间,那么就会导致多余的字符溢出到相邻的存储单位,这样后果是很严重的,所以我们进行了优化,在每个函数上添加了一个参数n,来进行限制

下面我们来分析一下这些函数:

strlen求字符串长度

函数原型:

size_t strlen ( const char * str );

代码模拟实现:

int my_strlen(const char * str)
{
    int count = 0;
    while (*str++ != '\0')
    {
        count++;
    }
    printf("%d", count);
}
strcat连接字符串

函数原型:

char * strcat ( char * destination, const char * source )

连接字符串:将参数二链接到参数一的结尾,从而使字符串成为一个新的字符串

返回值是第一个参数的值

该函数存在不安全性,他并不检查第一个数组是否能够容纳第二个数组,如果没有为第一个数组开辟足够大的空间,就会导致多余的字符溢出到相邻存储单元,就会出现问题。

代码模拟实现:

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

int main()
{
    char a[] = "hello,";
    char b[] = "world!";

    char *c = my_strcat(a, b);
    printf("%s", c);
    system("pause");
    return 0;
}
strncat连接字符串

函数原型:

char * strncat ( char * destination, const char * source, size_t num );

比strcat多了一个参数,指明最多允许添加的字符数目
如:strncat(str1,str2,5):该函数表示将str2追加到str1后面,直到加到第五个字符或遇到\0为止

模拟实现:

#include <stdio.h>  
#include <Windows.h>  
#include <assert.h>  
#include <string.h>  
char *my_strncat(char *dest, const char *src, size_t count)
{
    assert(dest);
    assert(src);
    char *ret = dest;
    while (*dest)
    {
        dest++;
    }
    while (count-- > 1)
    {
        *dest++ = *src++;
    }
    *dest = '\0';
    return ret;
}
int main()
{
    char a[80] = "hello,";
    char b[] = "world!";
    printf("Before: %s\n", a);

    my_strncat(a, b, 3);
    printf("After: %s\n", a);
    system("pause");
    return 0;
}
strcmp比较字符串

函数原型:

 int strcmp ( const char * s1, const char * s2 );

返回值:

  1. 当s1 < s2时,返回负数。
  2. 当s1 = s2时,返回0.
  3. 当s1 > s2时,返回正数

用该函数比较字符串时,一直比较到找到不同的相应字符,搜索可能要进行到字符串结尾处

strncmp比较字符串

函数原型:

int strncmp ( const char * str1, const char * str2, size_t num );

代码模拟实现:


#include <stdio.h>  
#include <Windows.h>  
#include <assert.h>  
#include <string.h>  
int my_strncmp(const char *str1, const char *str2, int count)
{
    assert(str1 && str2);
    while (count && (*str1 == *str2))
    {
        if (*str1 == '\0' || *str2 == '\0')
        {
            return 0;
        }
        str1++;
        str2++;
        count--;
    }
    return *str1 - *str2;
}

int main()
{
    char a[] = "abc";
    char b[] = "abcd";
    int len=strlen(a);
    int c = my_strncmp(a, b,len);
    printf("%d", c);
    system("pause");
    return 0;
}
strcpy复制字符串

函数原型:

char * strcpy ( char * destination, const char * source );

将source字符串赋值到destination中。

模拟实现


#include <stdio.h>  
#include <Windows.h>  
#include <assert.h>  
#include <string.h>  

char * my_strcpy(char * dst, const char * src)
{
    assert(dst&&src);

    char *ret = dst;
    while (*src != '\0')
    {
        *dst++ = *src++;
    }
    *dst = '\0';
    return ret;

}

int main()
{
    char a[] = "hello";
    char b[] = "hi";
    char *c = my_strcpy(a, b);
    printf("%s", c);
    system("pause");
    return 0;
}
strncpy复制字符串
char * strncpy ( char * destination, const char * source, size_t num )

模拟实现:


#include <stdio.h>  
#include <Windows.h>  
#include <assert.h>  
#include <string.h>  
char * my_strncpy(char * dst, const char * src,size_t num)
{
    assert(dst&&src);

    char *ret = dst;
    while (num && *src != '\0')
    {
        *dst++ = *src++;
        num--;
    }
    *dst = '\0';
    return ret;

}

int main()
{
    char a[] = "hello";
    char b[] = "1234";
    char *c = my_strncpy(a, b,2);
    printf("%s", c);
    system("pause");
    return 0;
}
strstr查找字符串

函数原型:

 char * strstr (char * str1, const char * str2 );

在字符串str1中查找tr2子串.
返回值:
1. 返回子串str2在str1中首次出现位置的指针.
2. 如果没有找到子串str2, 则返回NULL.
3. 如果子串str2为空串, 函数返回str1值.
代码模拟实现:


#include <stdio.h>  
#include <Windows.h>  
#include <assert.h>  
#include <string.h>  

char * my_strstr(char * dest, const char * src)
{
    char  *str1 = dest;
    char const *str2 = src;
    char  *start = NULL;
    assert(dest);
    assert(src);

    if (*str2 == '\0')   //查找的字符串为0时返回被查找的字符串   
        return dest;

    while (*str1)        //要查找的字符串存在时   
    {
        start = str1;
        while (*str1&&*str2 && (*str1 == *str2))
        {
            str1++;
            str2++;
        }
        if (*str2 == '\0')
        {
            return start;
        }
        str2 = src;
        str1 = start + 1;
    }
    return NULL;      //不存在返回NULL  
}

int main()
{
    char a[] = "hello";
    char b[] = "ll";
    char *c = my_strstr(a, b);
    printf("%s", c);
    system("pause");
    return 0;
}
strchr查找某字符在字符串中首次出现的位置

函数原型:

  char * strchr (       char * str, int character );
  • 参数解释:
    1. str表示要查找的字符串
    2. character表示要查找的字符
  • 返回值:
    如果找到指定的字符则返回该字符所在地址,否则返回 NULL。

总结,在这里我们需要特别注意的是,在模拟实现的时候一定要记得字符串结束加‘\0’,还有以上的函数只能用于字符串,不能用于别的类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值