一些常用字符串操作函数的内部实现

常用的字符串库函数的内部实现,截自linux内核中的lib/string.c文件,绝对标准的程序,供大家参考。

    
memset:
  1. /*
  2.  * memset - Fill a region of memory with the given value
  3.  * @s: Pointer to the start of the area.
  4.  * @c: The byte to fill the area with
  5.  * @count: The size of the area.
  6.  */
  7. void *memset(void *s, int c, size_t count)
  8. {
  9.     char *xs = s;

  10.     while (count--)
  11.         *xs++ = c;
  12.     return s;
  13. }

memcpy:
  1. /*
  2. * memcpy - Copy one area of memory to another
  3. * @dest: Where to copy to
  4. * @src: Where to copy from
  5. * @count: The size of the area.
  6. */
  7. void *memcpy(void *dest, const void *src, size_t count)
  8. {
  9. char *tmp = dest;
  10. const char *s = src;
  11. while (count--)
  12. *tmp++ = *s++;
  13. return dest;
  14. }

memmove:
  1. /*
  2.  * memmove - Copy one area of memory to another
  3.  * @dest: Where to copy to
  4.  * @src: Where to copy from
  5.  * @count: The size of the area.
  6.  * Unlike memcpy(), memmove() copes with overlapping areas.
  7.  */
  8. void *memmove(void *dest, const void *src, size_t count)
  9. {
  10.     char *tmp;
  11.     const char *s;

  12.     if (dest <= src) {
  13.         tmp = dest;
  14.         s = src;
  15.         while (count--)
  16.             *tmp++ = *s++;
  17.     } else {
  18.         tmp = dest;
  19.         tmp += count;
  20.         s = src;
  21.         s += count;
  22.         while (count--)
  23.             *--tmp = *--s;
  24.     }
  25.     return dest;
  26. }

memcmp:
  1. /*
  2.  * memcmp - Compare two areas of memory
  3.  * @cs: One area of memory
  4.  * @ct: Another area of memory
  5.  * @count: The size of the area.
  6.  */
  7. int memcmp(const void *cs, const void *ct, size_t count)
  8. {
  9.     const unsigned char *su1, *su2;
  10.     int res = 0;

  11.     for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  12.         if ((res = *su1 - *su2) != 0)
  13.             break;
  14.     return res;
  15. }

strcpy:
  1. /*
  2.  * strcpy - Copy a %NUL terminated string
  3.  * @dest: Where to copy the string to
  4.  * @src: Where to copy the string from
  5.  */
  6. char *strcpy(char *dest, const char *src)
  7. {
  8.     char *tmp = dest;

  9.     while ((*dest++ = *src++) != '\0');

  10.     return tmp;
  11. }

strncpy:
  1. /*
  2.  * strncpy - Copy a length-limited, %NUL-terminated string
  3.  * @dest: Where to copy the string to
  4.  * @src: Where to copy the string from
  5.  * @count: The maximum number of bytes to copy
  6.  *
  7.  * The result is not %NUL-terminated if the source exceeds
  8.  * @count bytes.
  9.  *
  10.  * In the case where the length of @src is less than that of
  11.  * count, the remainder of @dest will be padded with %NUL.
  12.  */
  13. char *strncpy(char *dest, const char *src, size_t count)
  14. {
  15.     char *tmp = dest;

  16.     while (count) {
  17.         if ((*tmp = *src) != 0)
  18.             src++;
  19.         tmp++;
  20.         count--;
  21.     }

  22.     return dest;
  23. }

strcat:
  1. /*
  2.  * strcat - Append one %NUL-terminated string to another
  3.  * @dest: The string to be appended to
  4.  * @src: The string to append to it
  5.  */
  6. char *strcat(char *dest, const char *src)
  7. {
  8.     char *tmp = dest;

  9.     while (*dest)
  10.         dest++;
  11.     while ((*dest++ = *src++) != '\0');

  12.     return tmp;
  13. }

strncat:
  1. /*
  2.  * strncat - Append a length-limited, %NUL-terminated string to another
  3.  * @dest: The string to be appended to
  4.  * @src: The string to append to it
  5.  * @count: The maximum numbers of bytes to copy
  6.  *
  7.  * Note that in contrast to strncpy(), strncat() ensures the result is
  8.  * terminated.
  9.  */
  10. char *strncat(char *dest, const char *src, size_t count)
  11. {
  12.     char *tmp = dest;

  13.     if (count) {
  14.         while (*dest)
  15.             dest++;
  16.         while ((*dest++ = *src++) != 0) {
  17.             if (--count == 0) {
  18.                 *dest = '\0';
  19.                 break;
  20.             }
  21.         }
  22.     }

  23.     return tmp;
  24. }

strcmp:
  1. /*
  2.  * strcmp - Compare two strings
  3.  * @cs: One string
  4.  * @ct: Another string
  5.  */
  6. int strcmp(const char *cs, const char *ct)
  7. {
  8.     unsigned char c1, c2;

  9.     while (1) {
  10.         c1 = *cs++;
  11.         c2 = *ct++;
  12.         if (c1 != c2)
  13.             return c1 < c2 ? -: 1;
  14.         if (!c1)
  15.             break;
  16.     }

  17.     return 0;
  18. }

strncmp:
  1. /*
  2.  * strncmp - Compare two length-limited strings
  3.  * @cs: One string
  4.  * @ct: Another string
  5.  * @count: The maximum number of bytes to compare
  6.  */
  7. int strncmp(const char *cs, const char *ct, size_t count)
  8. {
  9.     unsigned char c1, c2;

  10.     while (count) {
  11.         c1 = *cs++;
  12.         c2 = *ct++;
  13.         if (c1 != c2)
  14.             return c1 < c2 ? -: 1;
  15.         if (!c1)
  16.             break;
  17.         count--;
  18.     }

  19.     return 0;
  20. }

strchr:
  1. /*
  2.  * strchr - Find the first occurrence of a character in a string
  3.  * @s: The string to be searched
  4.  * @c: The character to search for
  5.  */
  6. char *strchr(const char *s, int c)
  7. {
  8.     for (; *!= (char)c; ++s)
  9.         if (*== '\0')
  10.             return NULL;

  11.     return (char *)s;
  12. }

strlen:
  1. /*
  2.  * strlen - Find the length of a string
  3.  * @s: The string to be sized
  4.  */
  5. size_t strlen(const char *s)
  6. {
  7.     const char *sc;

  8.     for (sc = s; *sc != '\0'; ++sc);

  9.     return sc - s;
  10. }

strnlen:
  1. /*
  2.  * strnlen - Find the length of a length-limited string
  3.  * @s: The string to be sized
  4.  * @count: The maximum number of bytes to search
  5.  */
  6. size_t strnlen(const char *s, size_t count)
  7. {
  8.     const char *sc;

  9.     for (sc = s; count-- && *sc != '\0'; ++sc);

  10.     return sc - s;
  11. }

strsep:
  1. /*
  2.  * strsep - Split a string into tokens
  3.  * @s: The string to be searched
  4.  * @ct: The characters to search for
  5.  *
  6.  * strsep() updates @s to point after the token, ready for the next call.
  7.  */
  8. char *strsep(char **s, const char *ct)
  9. {
  10.     char *sbegin = *s;
  11.     char *end;

  12.     if (sbegin == NULL)
  13.         return NULL;

  14.     end = strpbrk(sbegin, ct);
  15.     if (end)
  16.         *end++ = '\0';
  17.     *= end;

  18.     return sbegin;
  19. }

strstr:
  1. /*
  2.  * strstr - Find the first substring in a %NUL terminated string
  3.  * @s1: The string to be searched
  4.  * @s2: The string to search for
  5.  */
  6. char *strstr(const char *s1, const char *s2)
  7. {
  8.     int l1, l2;

  9.     l2 = strlen(s2);
  10.     if (!l2)
  11.         return (char *)s1;
  12.     l1 = strlen(s1);
  13.     while (l1 >= l2) {
  14.         l1--;
  15.         if (!memcmp(s1, s2, l2))
  16.             return (char *)s1;
  17.         s1++;
  18.     }

  19.     return NULL;
  20. }

另外加上itoa(),atoi(),atof()实现(非内部实现)

1、itoa函数实现

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. void itoa(int i, char *string)  
  4. {  
  5.         int power=0,j=0;  
  6.   
  7.         j=i;  
  8.         for( power=1;j>10;j/=10)  
  9.                 power*=10;  
  10.   
  11.         for(;power>0;power/=10)  
  12.         {  
  13.                 *string++='0'+i/power;  
  14.                 i%=power;  
  15.         }  
  16.         *string='\0';  
  17.         printf("%s\n",string);  
  18. }  
  19. void main()  
  20. {  
  21.         char string[20];  
  22.         itoa(12345, string);  
  23.         printf("%s\n",string);  
  24. }  

其中power相当于类似于1234,其power=1000;134,其power=100

*string++='0'+i/power;//获得取得字符的asicii码

i/power取得字符,例如1234/1000=1;234/100=2


2、atoi实现

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int atoi(char *str)  
  2. {  
  3.         if(!str)  
  4.                 return -1;  
  5.         bool bMinus=false;  
  6.         int result=0;  
  7.   
  8.         if(('0'>*str || *str>'9')&&(*str=='+'||*str=='-'))  
  9.         {  
  10.                if(*str=='-')  
  11.                 bMinus=true;  
  12.                *str++;  
  13.         }  
  14.         while( *str != '\0')  
  15.         {  
  16.                 if('0'> *str || *str>'9')  
  17.                         break;  
  18.                 else  
  19.                         result = result*10+(*str++ - '0');  
  20.         }  
  21.   
  22.         if (*str != '\0')//no-normal end  
  23.                 return -2;  
  24.   
  25.         return bMinus?-result:result;  
  26. }  

重写的atoi函数,没有考虑溢出的情况。

 if(('0'>*str || *str>'9')&&(*str=='+'||*str=='-'))//判读第一个字符是否为数字的正负号

if (*str != '\0')//no-normal end,当上文的while循环不正常退出,应视为字符串不合法,例如“+1234abc”

测试:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. char *c1 = "12345";  
  2.         char *c2 = "-12345";  
  3.         char *c3 = "bat-123";  
  4.         char *c4 = "+123abc";  
  5.   
  6.   
  7.         printf("c1=%d\n",atoi(c1));  
  8.         printf("c2=%d\n",atoi(c2));  
  9.         printf("c3=%d\n",atoi(c3));  
  10.         printf("c4=%d\n",atoi(c4));  

输出结果为:

c1=12345
c2=-12345
c3=-2
c4=-2


3.atof()

#include<stdio.h>
#include<string.h>
#define SIZE 50
double atof(char *s);
void main()
{
 char s[SIZE];
 printf("Input a string:\n");
 gets(s);
 atof(s);
}
double atof(char *s)
{
 int i,j,k=0;
 float total,head = 0.0,tail = 0.0;
 for(i = 0;i<strlen(s);i++)
 {
  k++;
  if(*(s+i)=='.')
   break;
 } 
 for(j=0;j<k-1;j++)
 {
  head = head*10 + *(s+j)-'0';
 }
 for(i=strlen(s)-1;i>=k;i--)
 {
  tail = tail/10.0 + (*(s+i)-'0')/10.0;
 }
 total = head + tail;
 printf("S after atof:%f",total);
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值