【C语言】字符串函数的实现

  • 求字符串的长度
    strlen
  • 长度不受限制的字符串函数
    strcpy
    strcat
    strcmp
  • 长度受限制的字符串函数
    strncpy
    strncat
    strncmp
  • 字符串查找
    strchr
    strrchr
    strpbrk
    strstr

  • 高级字符串查找
    strspn
    strcspn

求字符串的长度strlen

#include 
#include 

int my_strlen(const char *str)  //const在这里修饰是一大亮点 因为strlen函数只需计算字符串的长度,
{                               //无需更改,所以加上const修饰,不能随意改变
    int count = 0;
    assert(str != NULL);    // assert()在这里是第二亮点,断言的作用,判断指针是否为空指针
    while(*str != '\0')
    {
       count++;
       str++;
    }
    return count;
}
int main()
{
    char *p = "abcdef";
    int ch = my_strlen(p);
    printf("%d\n",ch);
    return 0;
}

长度不受限制的字符串

  • strcpy
#include 
#include 
#include 

char * my_strcpy(char * str_des, const char * str_source)
{
    assert(str_des);
    assert(str_source);
    char * ret = str_des;
    while (*ret++ = *str_source++)   
    {
        ;
    }
    return str_des;
}

int main()
{
    char str1[] = "hello world";
    char str2[20] = { 0 };    //将空间全部初始化为0
    char *str3 = my_strcpy(str2, str1);

    printf("%s\n", str2);
    printf("%s\n", str3);

    system("pause");
    return 0;
}
  • strcat
#include 
#include <assert.h>
#include 

char * my_strcat(char * str_source, char * str_des)
{
    assert(str_des);
    assert(str_source);
    char *p = str_source;
    while (*p)   //不能写成*p++当p开始为0还会++
    {
        p++;
    }
    while (*p++ = *str_des++);
    return str_des;
}

int main()
{
    char str[40] = { 0 };
    my_strcat(str,"hello ");
    printf("%s\n",str);
    my_strcat(str,"world!");
    printf("%s\n", str);
    system("pause");
    return 0;
}
  • strcmp字符串比较
#include 
#include 
#include 

//strcmp字符串比较
//两个字符串相等返回0,第一个大于第二个返回正数,反之负数
int my_strcmp(const char* str1, const char * str2)
{
    while (*str1 && *str2)
    {
        if (*str1 == *str2)
        {
            str1++;
            str2++;
        }
        else if (*str1 > *str2)
            return 1;
        else
            return -1;
    }
    if (*str1)
        return 1;
    if (*str2)
        return -1;
    return 0;
}

int main()
{
    char str1[] = "a";
    char str2[] = "b";

    int n = my_strcmp(str1, str2);
    printf("%d\n", n);
    system("pause");
    return 0;
}

长度受限制的字符串函数

  • strncpy 拷贝n个字符
#include 
#include 
#include 

char * my_strncpy(char * str_des, const char * str_source,int size)
{
    assert(str_des);
    assert(str_source);
    char * ret = str_des;    //定义一个指针要拷贝的空间首地址
    while (size--)
    {
        *ret++ = *str_source++;
    }
    return str_des;
}

int main()
{
    char str1[] = "hello world";
    char str2[20] = { 0 };    //将空间全部初始化为0
    char *str3 = my_strncpy(str2, str1,4);

    printf("%s\n", str2);
    printf("%s\n", str3);

    system("pause");
    return 0;
}
  • strncat
    将目标字符串的指定长度的字符与原字符串链接起来
#include 
#include <assert.h>
#include 

char * my_strncat(char * str_source, char * str_des,int size)
{
    assert(str_des);
    assert(str_source);
    char *p = str_source;
    while (*p)   //不能写成*p++当p开始为0还会++
    {
        p++;
    }
    while (size--)
    {
        *p++ = *str_des++;
    }
    return str_des;
}

int main()
{
    char str[40] = { 0 };
    my_strncat(str, "hello ",3);
    printf("%s\n", str);
    my_strcat(str, "world!",2);
    printf("%s\n", str);
    system("pause");
    return 0;
}
  • strncmp
    字符串前n个字符比较
#include 
#include 
#include 

int my_strncmp(const char* str1, const char * str2,int size)
{
    while (size--)
    {
        if (*str1 == *str2)
        {
            str1++;
            str2++;
        }
        else if (*str1 > *str2)
            return 1;
        else
            return -1;
    }
    return 0;
}

int main()
{
    char str1[] = "abcd";
    char str2[] = "abce";

    int n = my_strncmp(str1, str2,3);
    printf("%d\n", n);
    system("pause");
    return 0;
}

字符串查找

  • strchr
    找一个字符在字符串中出现的位置
#include 
#include 
#include 

//找一个字符在一个字符串出现的位置
char * my_strchr(const char *str, char p)
{
    while (*str)
    {
        if (*str == p)
            return str;
        str++;
    }
    return NULL;
}
int main()
{
    char str[] = "abcd";
    char * p = my_strchr(str,'x');
    printf("%s\n", p);
    system("pause");
    return 0;
}
  • strrchr
    找出字符串中一个字符最后一次出现的位置
#include 
#include 
#include 

char * my_strrchr(char * str, char x)
{
    char *p = str + strlen(str) - 1;
    while (p != str-1)
    {
        if (*p == x)
            return p;
        p--;
    }
    return NULL;
}

int main()
{
    char str[] = "This is a sample string ";
    char * ret = my_strrchr(str,'t');
    printf("%s\n", ret);
    system("pause");
    return 0;
}
  • strpbrk
    strpbrk返回一个指针,返回key任何一个字符第一次出现的位置
#include 
#include 
#include 

char * my_strpbrk(char * str,char*  key)
{
    char *s = str;
    char *k = key;
    while(*s)
    {
        while (*k)
        {
            if (*s == *k)
                return s;
            k++;
        }
        s++;
        k = key;
    }
    return NULL;
}

int main()
{
    char str[] = "This is a sample string ";
    char key[] = "aeiou";
    char *pch = my_strpbrk(str,key);
    while (pch != NULL)
    {
        printf("%c ", *pch);
        pch = my_strpbrk(pch+1, key);
    }
    system("pause");
    return 0;
}
  • strstr

在字符串str1中找出str2子串,返回str1中str2的位置

#include 
#include 
#include 

strstr在字符串str1中找出str2子串,返回str1中str2的位置
char* my_strstr(char * str1, char * str2)
{
    char *pa1 = str1;
    char *pb1 = pa1;
    char *pa2 = str2;
    if (str2 == NULL)
        return NULL;
    while (*pa1)
    {
        pb1 = pa1;
        pa2 = str2;
        while (*pb1 && *pa2 && *pb1 == *pa2)
        {
            pb1++;
            pa2++;
        }
        if ('\0' == *pa2)
            return pa1;
        pa1++;
    }
    return NULL;
}


int main()
{
    char str1[] = "123abc889";
    char str2[] = "7";
    char *p = my_strstr(str1, str2);
    printf("%s\n", p);
    system("pause");
    return 0;
}

高级查找

  • strspn
    返回字符串str1中的字符在字符串str2中出现的个数
int my_strspn(char * str1, char * str2)
{
    int count = 0;
    char * p2 = str2;
    while (*str1)
    {
        while (*p2)
        {
            if (*str1 == *p2)
            {
                count++;
                break;
            }
            p2++;
        }
        p2 = str2;
        str1++;
    }
    return count;
}


int main()
{
    char str1[] = "23d";
    char str2[] = "1234abcd";
    int n= my_strspn(str1, str2);
    printf("%d\n", n);
    system("pause");
    return 0;
}


  • strcspn

strcspn函数 遍历str1中的字符,直到遇到str2中的字符停下来,返回str1遇到之前遍历字符数
可以使用strcspn函数,返回str2中字符在str1中第一次出现的地址,用该地址减去str2的地址就是
遍历的字符数
int my_strcspn(char * str1, char *str2)
{
    char *p = strpbrk(str1, str2);
    return p - str1;
}

int main()
{
    char str1[] = "123abc";
    char str2[] = "7a";
    int n = my_strcspn(str1, str2);
    printf("%d\n", n);
    system("pause");
    return 0;
}

你可能感兴趣的:(c语言)