重写strstr、strcpy、memcpy、memset、atof算法

  1 #include<stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <ctype.h>
  5 #include <math.h>
  6 
  7 char *Mystrstr(const char *string, const char *strCharSet)
  8 {
  9     if (NULL == string)
 10     {
 11         return (char *)string;
 12     }
 13     if (NULL == strCharSet)
 14     {
 15         return NULL;
 16     }
 17     int nLen1 = strlen(string);
 18     int nLen2 = strlen(strCharSet);
 19 
 20     for (int i = 0; i < nLen2; i++)
 21     {
 22         for (int j = 0; j <nLen1; j++)
 23         {
 24             if (strCharSet[i] == string[j])
 25             {
 26                 int nPos = j;
 27                 while ( (i < nLen2) &&(strCharSet[i] == string[j]))
 28                 {
 29                     i++;
 30                     j++;
 31                 }
 32                 if (i == nLen2)
 33                 {
 34                     return (char *)&string[nPos];
 35                 }
 36                 i = nPos;
 37             }
 38         }
 39     }
 40     return NULL;
 41 }
 42 
 43 char *Mystrcpy( char *strDestination, const char *strSource )
 44 {
 45     if (strDestination == NULL && strDestination == NULL)
 46     {
 47         exit(0);
 48     }
 49     
 50     while (*strSource != '\0')
 51     {
 52         *strDestination++ = *strSource++;
 53     }
 54     *strDestination = '\0';
 55     return strDestination;
 56 }
 57 
 58 void *Mymemset( void *dest, int c, size_t count )
 59 {
 60     char *pszDest = (char *)dest;
 61     if (pszDest == NULL)
 62     {
 63         exit(0);
 64     }
 65     if (0 == count)
 66     {
 67         return dest;
 68     }
 69     for (size_t i = 0; i < count; i++)
 70     {
 71         *pszDest++ = c;
 72     }
 73     return dest;
 74 }
 75 
 76 void* MyMemcpy( void *dest, const void *src, int nCount )
 77 {
 78     if (NULL == dest || src == NULL)
 79     {
 80         return NULL;
 81     }
 82     char *pszDest = (char *)dest;
 83     const char *pszSrc = (char *)src;
 84     
 85 
 86     if (pszDest > pszSrc && pszDest < pszSrc + nCount)
 87     {//有重复(pszDest的位置 在pszSrc里面)
 88         for (int i = nCount - 1; i >= 0; i--)
 89         {//从后往前拷贝
 90             pszDest[i] = pszSrc[i];
 91         }
 92     }
 93     else
 94     {
 95         for (int i = 0; i < nCount; i++)
 96         {
 97             pszDest[i] = pszSrc[i];
 98         }
 99     }
100     return dest;
101 }
102 
103 
104 
105 double Myatof( const char *string )
106 {
107     bool IsNegative = false;
108     bool IsInt = true;
109     double dblResult = 0;
110     int i = 1;
111     while (*string != '\0')
112     {
113         switch(*string) {
114             case ' ':
115                 string++;
116                 break;
117             case '-':
118                 IsNegative = true;
119                 string++;
120                 break;
121             case '+':
122                 IsNegative = false;
123                 string++;
124                 break;
125             case '.':
126                 IsInt = false;
127                 string++;
128                 break;
129             default:
130                 if (IsInt)
131                 {
132                     dblResult = dblResult*10 + (*string - '0');
133                     string++;
134                 }
135                 else
136                 {
137                     dblResult += (*string - '0') / pow(10, i++); 
138                     string++;
139                 }
140                 break;
141          }//end_of_switch
142         
143     }//end_of_while
144     
145     return IsNegative? -dblResult : dblResult;
146 }
147 
148 
149 int main()
150 {
151     char szBuf[21];
152     memset(szBuf, 0, sizeof(szBuf));
153     for (int i = 0; i < 20; i++)
154     {
155         szBuf[i] = 'a' + i;
156     }
157     MyMemcpy(&szBuf[10], &szBuf[2], 4);
158     printf("%s\n", szBuf);
159     return 0;
160 }

 

转载于:https://www.cnblogs.com/nothx/p/8512341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. atof(): 将字符串转换为double类型的值。 例如: ```c++ char str[] = "3.14"; double num = atof(str); printf("%f", num); ``` 结果为:3.140000 2. atoi(): 将字符串转换为int类型的值。 例如: ```c++ char str[] = "1234"; int num = atoi(str); printf("%d", num); ``` 结果为:1234 3. atol(): 将字符串转换为long类型的值。 例如: ```c++ char str[] = "1234567"; long num = atol(str); printf("%ld", num); ``` 结果为:1234567 4. strtod(): 类似于atof(),将字符串转换为double类型的值。 例如: ```c++ char str[] = "3.14"; double num = strtod(str, NULL); printf("%f", num); ``` 结果为:3.140000 5. strtol(): 将字符串转换为long类型的值,同时支持指定转换的基数(例如10进制、16进制等)和错误检查。 例如: ```c++ char str[] = "0110"; long num = strtol(str, NULL, 2); printf("%ld", num); ``` 结果为:6 6. strtoul(): 类似于strtol(),不过返回的是无符号的long类型。 例如: ```c++ char str[] = "0xA"; unsigned long num = strtoul(str, NULL, 16); printf("%lu", num); ``` 结果为:10 7. memset(): 将一段内存区域设置为指定的值。 例如: ```c++ char str[10]; memset(str, 'a', sizeof(str)); printf("%s", str); ``` 结果为:aaaaaaa 8. memcpy(): 将一段内存区域的内容复制到另一段内存区域。 例如: ```c++ char src[] = "hello"; char dst[10]; memcpy(dst, src, sizeof(src)); printf("%s", dst); ``` 结果为:hello 9. memmove(): 和memcpy()类似,但是保证在有重叠的情况下会正确工作。 例如: ```c++ char str[] = "hello"; memmove(str + 2, str, 3); printf("%s", str); ``` 结果为:hehlo 10. memcmp(): 比较两段内存区域的内容是否相等。 例如: ```c++ char str1[] = "hello"; char str2[] = "Hello"; int result = memcmp(str1, str2, 5); printf("%d", result); ``` 结果为:32(h和H的ASCII码差值) 11. memchr(): 在一段内存区域中搜索指定的字符,并返回指向该字符的指针。 例如: ```c++ char str[] = "hello"; char* ptr = (char*)memchr(str, 'l', 5); printf("%s", ptr); ``` 结果为:ll 12. strcpy(): 将一个字符串复制到另一个字符串。 例如: ```c++ char src[] = "hello"; char dst[10]; strcpy(dst, src); printf("%s", dst); ``` 结果为:hello 13. strncpy(): 类似于strcpy(),不过只会复制指定长度的字符。 例如: ```c++ char src[] = "hello"; char dst[10]; strncpy(dst, src, 3); dst[3] = '\0'; printf("%s", dst); ``` 结果为:hel 14. strcat(): 将一个字符串附加到另一个字符串的末尾。 例如: ```c++ char str1[] = "hello"; char str2[] = "world"; strcat(str1, str2); printf("%s", str1); ``` 结果为:helloworld 15. strncat(): 类似于strcat(),不过只会附加指定长度的字符。 例如: ```c++ char str1[] = "hello"; char str2[] = "world"; strncat(str1, str2, 3); printf("%s", str1); ``` 结果为:helloworld 16. strcmp(): 比较两个字符串是否相等。 例如: ```c++ char str1[] = "hello"; char str2[] = "world"; int result = strcmp(str1, str2); printf("%d", result); ``` 结果为:-15 17. strncmp(): 类似于strcmp(),不过只会比较指定长度的字符。 例如: ```c++ char str1[] = "hello"; char str2[] = "world"; int result = strncmp(str1, str2, 3); printf("%d", result); ``` 结果为:0 18. strchr(): 在一个字符串中搜索指定的字符,并返回指向该字符的指针。 例如: ```c++ char str[] = "hello"; char* ptr = strchr(str, 'l'); printf("%s", ptr); ``` 结果为:llo 19. strrchr(): 类似于strchr(),不过会从字符串的末尾开始搜索。 例如: ```c++ char str[] = "hello"; char* ptr = strrchr(str, 'l'); printf("%s", ptr); ``` 结果为:lo 20. strstr(): 在一个字符串中搜索指定的子字符串,并返回指向该子字符串的指针。 例如: ```c++ char str[] = "hello world"; char* ptr = strstr(str, "world"); printf("%s", ptr); ``` 结果为:world
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值