C语言常见字符串函数实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言常见字符串函数实现相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int my_strlen(char *s)
{
    assert(s!=NULL);
    int count=0;
    while(*s!=‘\0‘)
    {
         s++;
         count++;
    }
    return count;
}
char *my_strcpy(char *s1,const char *s2)
{
    assert(s1!=NULL);
    assert(s2!=NULL);
    char *res=s1;
    while(*s2!=‘\0‘)
    {
        *s1=*s2;
        s1++;//s1,s2均指向未赋值的下一个字节
        s2++;
    }
    *s1=‘\0‘;
    return res;
}
char *my_strncpy(char *s1,const char *s2,int n)
{
    assert(s1!=NULL);
    assert(s2!=NULL);
    char *res=s1;
    while(*s2!=‘\0‘&&n)
    {
        *s1=*s2;
        s1++;
        s2++;
        n--;
    }
    *s1=‘\0‘;
    return res;
}
char *my_strchr(char *str,int c)
{
    assert(str!=NULL);
    while(*str!=‘\0‘)
    {
        if(*str==(char)c)
            return str;
        else
            str++;
    }
    return NULL;
}
char *my_strstr(const char *str,char *sub)
{
   assert(str!=NULL&&sub!=NULL);
   char *s=str;
   char *t=sub;
   int len=0;
   for(;*s!=‘\0‘;s++)
   {
      while(*t!=‘\0‘&&*s==*t)
      {
          s++;
          t++;
          len++;
      }
        if(*t==‘\0‘)
            return s-len;
   }
   return NULL;
}
char *my_strcat(char *s1,const char *s2)
{
    assert(s1!=NULL);
    assert(s2!=NULL);
    char *res=s1;
    while(*s1!=‘\0‘)
        s1++;
    while(*s2!=‘\0‘)
    {
        *s1=*s2;
        s1++;
        s2++;
    }
    *s1=‘\0‘;
    return res;
}
int my_strcmp(const char *s1,const char *s2)
{
    assert(s1!=NULL);
    assert(s2!=NULL);
    while(*s1!=‘\0‘&&*s2!=‘\0‘&&*s1==*s2)
    {
        s1++;
        s2++;
    }
    return *s1-*s2;
}
char *my_strdup(char *s)
{
    assert(s!=NULL);
    int len=0;
    char *dup=(char*)malloc(sizeof(char)*(len+1));
    if(NULL==dup)
        return NULL;
    while(*s!=‘\0‘)
    {
        *dup=*s;
        dup++;
        s++;
        len++;
    }
    *dup=‘\0‘;
    return dup-len;
}
void *my_memset(void* buffer,int c,int n)
{
   assert(buffer!=NULL);
   char *p=(char*)buffer;
   while(n--)
   {
        *p=(char)c;
        p++;
   }
   return  buffer;
}
//拷贝内存块,不考虑内存重叠
void *my_memcpy(void *to,const void *from,int n)
{
   assert((to!=NULL)&&(from!=NULL));
   char *res=(char *)to;
   char *t=(char*)to;
   char *f=(char*)from;
   assert((t<f)||(t>f-n+1));
   while(n--)
   {
       *t=*f;
       t++;
       f++;
   }
   return res;
}
void *my_memmove(void *to,void *from,int count)
{
   assert((to!=NULL)&&(from!=NULL));
   char *res=(char*)to;
   char *des=(char*)to;
   char *src=(char*)from;
   if(des<src||des>src+count-1)
   {
       while(count--)
       {
           *des=*src;
           des++;
           src++;
       }
   }
   else
   {
       des=to+count-1;
       src=from+count-1;
       while(count--)
       {
           *des=*src;
           des--;
           src--;
       }
   }
   return res;
}
int main()
{
    //my_strlen
    char str[10]="strlen";
    int len=my_strlen(str);
    printf("my_strlen:%d\n",len);
    //my_strcpy  my_strncpy
    char s1_cpy[10]="str";
    char s2_cpy[10]="cpy";
    char *s_cpy=my_strcpy(s1_cpy,s2_cpy);
    printf("%s\n",s_cpy);
    //my_strncpy
    char s1_ncpy[20]="strncpy";
    char s2_ncpy[10]="strn";
    char *s_ncpy=my_strncpy(s1_ncpy,s2_ncpy,3);
    printf("%s\n",s_ncpy);
    //my_strcat
    char s1_cat[20]="strcat";
    char s2_cat[10]="after";
    char *s_cat=my_strcat(s1_cat,s2_cat);
    printf("%s\n",s_cat);
    //my_strcmp
    char s1_cmp[10]="cmp1";
    char s2_cmp[10]="cmp2";
    printf("%d\n",my_strcmp(s1_cmp,s2_cmp));
    //my_strchr
    char s_chr[10]="strchr";
    char *p_chr=my_strchr(s_chr,‘t‘);
    if(NULL!=p_chr)
    printf("%s\n",p_chr);
    else
    printf("don‘t have this character.\n");
    //my_strdup
    char s_d[10]="strdup";
    char *s_dup=my_strdup(s_d);
    printf("%s\n",s_dup);
    free(s_dup);
    //my_strstr
    char s_str[20]="abcdrgrkj";
    char t_str[10]="bcd";
    printf("%s\n",my_strstr(s_str,t_str));
    //my_memset
    char s_mem[10]="hello";
    char *p_mem=my_memset(s_mem,‘*‘,strlen(s_mem));
    char s_mem1[10]="hello";
    char *p_mem1=my_memset(s_mem1,‘*‘,sizeof(s_mem1));
    printf("%s\n",p_mem);
    printf("%s\n",p_mem1);
    //my_memcpy
    char s_memcpy[10]="abcd";
    char *p=my_memcpy(s_memcpy,s_memcpy+2,2);//未发生内存重叠
    printf("%s\n",p);
    char s1_memcpy[10]="abcd";
    char *p1=my_memcpy(s1_memcpy+2,s1_memcpy,3);//未处理内存重叠,输出aba
    printf("%s\n",p1);
    //my_memmove
    char s_move[10]="abcd";
    char *p_m=my_memmove(s_move+2,s_move,3);//正确处理内存重叠,输出abc
    printf("%s\n",p_m);
    return 0;
}

  

以上是关于C语言常见字符串函数实现的主要内容,如果未能解决你的问题,请参考以下文章

C语言 | 常见数据转化函数

c语言深入浅出,玩爆常见字符串,内存操作库函数(爆肝最长时间之作)

c语言字符处理函数常见使用集合

嵌入式开发中常见3个的C语言技巧

#yyds干货盘点# C语言字符串与内存库函数的介绍与模拟实现

C语言常见问题总结