Linux中strchr与strstr函数实现。
来源:互联网 发布:淘宝hd 5.0.1 ios 编辑:程序博客网 时间:2024/05/16 11:25
<span style="font-size:18px;"><strong></strong></span>
</pre><pre code_snippet_id="354054" snippet_file_name="blog_20140520_2_3457534" name="code" class="cpp"><p>#ifndef __HAVE_ARCH_STRCHR</p>/**strchr - Find the first occurrence of a character in a string * @s: The string to be searched * @c: The character to search for */char *strchr(const char *s, int c)//定义函数接口的方式学一下,{for (; *s != (char)c; ++s)if (*s == '\0')return NULL;return (char *)s;}EXPORT_SYMBOL(strchr);
————————————————————————————————————————————————
上面中把强制类型中放在for中,则每次都要强制一次,可以把char ctemp=(char)c;
for(;ctemp!=*s;++s)if(*s=='\0')return NULL;return (char*) s;
上面中把强制类型中放在for中,则每次都要强制一次,可以把char ctemp=(char)c;
for(;ctemp!=*s;++s)if(*s=='\0')return NULL;return (char*) s;
________________________________________________________
同时还要注意一下for中的++s,虽然效果与s++一样,但效率可能不一样,
在编译器没有优化的情况下s++,表达式是i加1之前的副本,因为要存储这个副本,所以会多一些开销。单独在for中的s++;是个表达式,这个表达式与i相关的,
即表达式的值为i,但这个表达式之后,i为i+1;
而++i;这个表达式的i是直接加1,也为i本身。表达式的值与i的值相同。
http://blog.csdn.net/zy1691/article/details/4849808
——————————————————————————————————————
static inline char *strchr(const char *s, int c){char sc, ch = c;for (; (sc = *s++) != ch; ) {if (!sc)return NULL;}return (char *)s - 1;}
——————————————————————————————————————————————
*** strstr - Find the first substring in a %NUL terminated string* @s1: The string to be searched* @s2: The string to search for*/char *strstr(const char *s1, const char *s2){int l1, l2;l2 = strlen(s2);if (!l2)return (char *)s1;l1 = strlen(s1);while (l1 >= l2) {l1--;if (!memcmp(s1, s2, l2))return (char *)s1;s1++;}return NULL;}————————————————————————————————————————————————另一种方式#ifndef HAVE_STRSTRchar *strstr(char *s1, char *s2){ /* from libiberty */char *p;int len = strlen(s2); if (*s2 == '\0') /* everything matches empty string */return s1;for (p = s1; (p = strchr(p, *s2)) != NULL; p = strchr(p + 1, *s2)) {if (strncmp(p, s2, len) == 0)return (p);}return NULL;}#endif
<pre code_snippet_id="354054" snippet_file_name="blog_20140520_7_5009612">
<pre code_snippet_id="354054" snippet_file_name="blog_20140520_18_5249713" name="code" class="cpp"><pre code_snippet_id="354054" snippet_file_name="blog_20140520_18_5249713">
0 0
- Linux中strchr与strstr函数实现。
- 自己实现strstr函数与strchr函数
- strchr(),strstr()函数实现
- strchr与strstr函数
- strchr与strstr函数
- strchr与strstr函数
- linux字符串函数strchr strstr
- 模拟实现strchr,strrchr,strstr,strrstr函数
- C中字符串操作函数strstr strchr
- strchr和strstr函数
- 十 Strstr 与 strchr
- strstr函数和strchr函数
- 字符串函数---strstr()、memchr()、strchr()详解及实现
- 字符串处理函数strchr和strstr的实现
- 模拟实现strlen/strcpy/strcat/strcmp/strstr/strchr/函数
- 字符串函数---strstr()、memchr()、strchr()详解及实现
- c,c++中字符串处理函数strtok,strstr,strchr,strsub
- strcpy、strchr、strstr的实现
- linux vps CentOS LNMP系统上directadmin的完整安装教程
- C++简易字符串处理类
- 计算机科学中最重要的32个算法
- Cocos2d-x3.0 Json解析
- 作业8
- Linux中strchr与strstr函数实现。
- CSDN第一天
- android 绑定远程服务
- 第五周作业
- HTM5之WEB Worker
- linux平台下 延迟工作队列实例
- 行为型模式3:迭代器模式
- mysql学习 常用语句1
- 职员有薪水了