通过代码实现str系列函数

阅读: 评论:0

通过代码实现str系列函数

通过代码实现str系列函数

       size_t strlen(const char *s);

  The  strlen()  function calculates the length of the string pointed to by s,
       excluding the terminating null byte ('').

字符串都是以''结尾,没遇到''时自加


typedef unsigned int size_t;size_t my_strlen(char *string)
{size_t    len=0;assert(string);while(string[len]!=''){len++;}return len;
}

这个函数的实现虽然简单但是接下来的函数实现都会有他的身影希望大家认真学习次函数

char *strstr(const char *haystack, const char *needle);

The  strstr() function finds the first occurrence of the substring needle in
       the string haystack.  The terminating null bytes ('') are not compared.

char *my_strstr(const char *str1, const char *str2)
{assert(str1!= NULL);assert(str2!= NULL);int i = 0;int j = 0;if(*str2!=''){while(*str1!=''){for(i=0; *(str1+i)==*(str2+1); i++){if(!*(str2+1)){return (char *)str1;}}str1++;}return NULL;}return NULL;}

strstr函数实现思维:找到src在dest里的位置后返回此时src里dest的首地址。

 例:经典笔试题:实现函数查找str1中 str2 最后一次出现的位置并返回

char *my_str_strcmp(char *str1, const char *str2)
{assert(str1!= NULL);assert(str2!= NULL);int i = 0;int j = 0;if(*str2!=''){while(str1[i]!= ''){i++;}str1+=i-1;//将指针指向str1 末尾开始倒序查找while(*str1!=''){for(i=0; *(str1+i)==*(str2+i); i++){if(!*(str2+i+1)){return (char *)str1;}}str1--;}return NULL;}return NULL;
}

       char *strcat(char *dest, const char *src);

 The strcat() function appends the src string to the dest string, overwriting
       the terminating null byte ('') at the end of dest, and then adds a  termi‐
       nating  null  byte.   The  strings may not overlap, and the dest string must
       have enough space for the result.  If dest is not large enough, program  be‐
       havior is unpredictable; buffer overruns are a favorite avenue for attacking
       secure programs.

char * my_strcat(char *dest, const char *src)
{assert(dest!= NULL);assert(src!= NULL);int i = 0;int j = 0;while(dest[i] !=''){i++;}while(src[j]!=''){dest[i+j]=src[j];j++;}dest[i+j]='';return dest;
}

 strcat函数实现思维:strcat()函数用于将两个字符串拼接,str1,str2。字符串都是以结尾,所以将str1的''替换为str2的首字符最后以结束 

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

 The strcmp() function compares the two strings s1 and s2.  The locale is not
       taken into account (for a locale-aware  comparison,  see  strcoll(3)).   The
       comparison is done using unsigned characters.
 


int my_strcmp(char *dest, const char *src)
{assert(dest!= NULL);assert(src!= NULL);int i = 0;int j = 0;while(dest[i] == src[i])//判断dest 和 src是否相等{i++;if(dest[i]=='')break;}return *dest-*src;//返回首元素的ascll码值的插值,返回0代表两元素相等,其他不相等
}

strcmp函数实现思维:strcmp()函数用于比较两字符串是否一样,返回两字符首地址的ascll码值之差

       char *strcpy(char *dest, const char *src);

The  strcpy()  function  copies  the string pointed to by src, including the
       terminating null byte (''), to the buffer pointed to by dest.  The strings
       may not overlap, and the destination string dest must be large enough to re‐
       ceive the copy.  Beware of buffer overruns!  (See BUGS.)
 


char * my_strcpy(char *dest, const char *src)
{assert(dest!= NULL);assert(src!= NULL);int i = 0;while(src[i] !='')//求src长度,做字符替换{dest[i]=src[i];i++;}dest[i]='';return dest;
}

strcpy函数实现:求src长度后做字符替换

        char *strncpy(char *dest, const char *src, size_t n);

char * my_strncpy(char *dest, const char *src, int num)
{assert(dest!= NULL);assert(src!= NULL);assert(num > 0);int i = 0;while(src[i] !=''&&num>0)//求src长度,做字符替换{dest[i]=src[i];i++;num--;}dest[i]='';return dest;
}

 strncpy函数实现:在styrcpy的基础上加入一个限制条件num。只copy src字符串的前num个非""字符

       char *strncat(char *dest, const char *src, size_t n);


char * my_strncat(char *dest, const char *src, int num)
{assert(dest!= NULL);assert(src!= NULL);assert(num > 0);int i = 0;int j = 0;while(dest[i] !=''){i++;}while(src[j]!=''&&num > 0){dest[i+j]=src[j];j++;num--;}dest[i+j]='';return dest;
}

  strncat函数实现:在styrcat的基础上加入一个限制条件num。只连接 src字符串的前num个非""字符

 

本文发布于:2024-02-02 13:51:34,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170685309444228.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:华为
标签:函数   代码   系列   str
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23