手写代码必备手册(C版) —— 读书笔记 1、字符串 API
2015-08-23 09:25
513 查看
</pre>现场编写 <span style="font-family:LMMono10-Regular; font-size:9pt">strcpy, strlen, strstr, atoi<span style="font-family:AdobeSongStd-Light; font-size:9pt">等库函数<br style="orphans:2; widows:2" /></span></span><p></p><pre name="code" class="cpp">typedef unsigned int size_t
1、1 strlen
实现
strlen,获取字符串长度,函数原型如下:
size_t strlen(const char *str);
<span style="font-family: LMMono9-Regular;">size_t</span> strlen ( const char * str ) { const char *eos = str; while( *eos++ ) ; return( eos - str - 1 ); }
1.2 strcpy
实现
strcpy,字符串拷贝函数,函数原型如下:
char* strcpy(char *to, const char *from);
char* strcpy(char *to, const char *from) { assert(to != NULL && from != NULL); char *p = to; while ((*p++ = *from++) != '\0') ; return to; }
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"> </span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"><span style="font-family: LinLibertineOZ; font-size: 11pt;">1.3 strstr<br style="orphans: 2; text-align: -webkit-auto; widows: 2;" /> </span></span><pre name="code" class="cpp">实现 strstr,子串查找函数,函数原型如下: char * strstr(const char *haystack, const char *needle);
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></span>
char * strstr ( const char * str1, const char * str2 ) { char *cp = (char *) str1; char *s1, *s2; if ( !*str2 ) return((char *)str1); while (*cp) { s1 = cp; s2 = (char *) str2; while ( *s1 && *s2 && !(*s1-*s2) ) s1++, s2++; if (!*s2) return(cp); cp++; } return(NULL); }
</pre><p></p><p><span style="font-family: LinLibertineOZ; font-size: 11pt;"></span></p><span style="font-family:LinLibertineOZ; font-size:11pt"><strong>1.4 atoi</strong><span style="font-family:AdobeHeitiStd-Regular; font-size:9pt"><span style="font-family:AdobeSongStd-Light; font-size:9pt">实现 <span style="font-family:LMMono10-Regular; font-size:9pt">atoi<span style="font-family:AdobeSongStd-Light; font-size:9pt">,将一个字符串转化为整数,函数原型如下:<span style="font-family:LMMono9-Regular; font-size:8pt">int atoi(const char *str);</span></span></span></span></span></span><p><span style="font-family:LinLibertineOZ; font-size:11pt"><span style="font-family:LinLibertineOZ; font-size:11pt"><span style="font-family:AdobeHeitiStd-Regular; font-size:9pt"><span style="font-family:AdobeSongStd-Light; font-size:9pt"><span style="font-family:LMMono10-Regular; font-size:9pt"><span style="font-family:AdobeSongStd-Light; font-size:9pt"><span style="font-family:LMMono9-Regular; font-size:8pt"><span style="font-family:AdobeHeitiStd-Regular; font-size:9pt">分析</span></span></span></span><br style="orphans:2; widows:2" /></span></span></span><span style="font-family:AdobeSongStd-Light; font-size:9pt">注意,这题是故意给很少的信息,让你来考虑所有可能的输入。</span></span></p><p></p><div style="orphans:auto; widows:1"><span style="font-family:AdobeSongStd-Light"></span></div><span style="font-family:LinLibertineOZ; font-size:11pt"><span style="font-family:AdobeSongStd-Light; font-size:9pt"><span style="font-family:AdobeSongStd-Light; font-size:9pt">注意几个测试用例:<span style="font-family:LinLibertineO; font-size:9pt">1. <span style="font-family:AdobeSongStd-Light; font-size:9pt">不规则输入,但是有效, <span style="font-family:LinLibertineO; font-size:9pt">”-3924x8fc”<span style="font-family:AdobeSongStd-Light; font-size:9pt">,<span style="font-family:LinLibertineO; font-size:9pt">” + 413”,<span style="font-size:9pt">2. <span style="font-family:AdobeSongStd-Light; font-size:9pt">无效格式, <span style="font-family:LinLibertineO; font-size:9pt">” ++c”, ” ++1”<span style="font-size:9pt">3. <span style="font-family:AdobeSongStd-Light; font-size:9pt">溢出数据, <span style="font-family:LinLibertineO; font-size:9pt">”2147483648”</span></span></span><br style="orphans:2; widows:2" /></span></span></span></span></span></span></span></span></span></span></span><pre name="code" class="cpp"><pre name="code" class="cpp">int atoi(const char *str) { int num = 0; int sign = 1; const int len = strlen(str); int i = 0; while (str[i] == ' ' && i < len) i++; if (str[i] == '+') i++; if (str[i] == '-') { sign = -1; i++; } for (; i < len; i++) { if (str[i] < '0' || str[i] > '9') break; if (num > INT_MAX / 10 || (num == INT_MAX / 10 && (str[i] - '0') > INT_MAX % 10)) { return sign == -1 ? INT_MIN : INT_MAX; } num = num * 10 + str[i] - '0'; } return num * sign; }
相关文章推荐
- C/C++编译器与链接器工作原理
- 【C语言】C语言预处理命令总结
- c/c++ 求字符数组长度(非所占内存大小)
- [Java]Leetcode236 Lowest Common Ancestor of a Binary Tree
- 零基础学python-7.6 字符串格式化表达式
- 零基础学python-7.6 字符串格式化表达式
- 基于七牛Python SDK写的一个同步脚本
- 汉诺塔算法(递归)
- 我的php学习之SQL安全
- Java设计模式(四) 装饰 代理模式
- 第二节:springmvc传值方式
- matlab 给定函数 拟合
- 【java】itoo项目实战之EJB中的RMI框架如何设计
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
- php 5.6 与之前版本不兼容中的数组属性定义辨析
- 解决mac中wxpython对64位的支持
- 为什么我说Rust是靠谱的编程语言
- [转载]Windows下用g++ MATLAB里面用mex
- matlab 画二维折线图
- python基础教程学习笔记 — 字符编码问题