strstr和memcmp函数的实现
#include <stdio.h>
#include <stdlib.h> //malloc()函数
typedef unsigned int size_t; size_t my_strlen(const char * str)
{
const char *sc = NULL;
if(str == NULL)
return 0;
for(sc = str;*sc != '\0';sc++)
{
/* do nothing */
}
return sc - str;
} /* 因为类型可以为任意,所以形参应为void *
* 相等则返回0,否则不为0
*/
int my_memcmp(const void *s1,const void *s2,size_t count)
{
int res = 0;
const unsigned char *p1 =(const unsigned char *)s1;//注意是unsigned char *
const unsigned char *p2 =(const unsigned char *)s2;
for(p1 ,p2;count > 0;p1++,p2++,count--)
if((res =*p1 - *p2) != 0) //不相当则结束比较
break;
return res;
}
/* 查找字符串s2是否为s1的子串,s1为主串
* 如果是则返回从第一个子串开始的字符串
*/
char * my_strstr(const char *s1,const char *s2)
{
int len1,len2;
len2 = my_strlen(s2); //获取子串s2的长度
if(!len2) //如果子串s2为空则返回s1
return (char *)s1; //先强制类型转换
len1 = my_strlen(s1); //获取子串s1的长度
while(len1 >= len2)
{
len1--;
if(!my_memcmp(s1,s2,len2)) //my_memcmp返回0表示s1中存在s2的子串
return (char *)s1; //先强制类型转换
s1++;
}
return NULL; //len1 < len2时返回空
} int main()
{
printf("%s\n",my_strstr("hello world","world"));
printf("%s\n",my_strstr("hello world","e"));
printf("%s\n",my_strstr("hello world","llo"));
return 0;
}
执行结果:
2013年10月10日17:23分补充下面算法
不使用库函数来实现strstr函数,效率其实也不高,高效率应该使用KMP法
#include <stdio.h> char* strstr(char* buf, char* sub)
{
char* bp;
char* sp;
if(sub == NULL)
return buf;
while(buf !=NULL)
{
bp=buf;
sp=sub;
do{
if(!*sp) //sp到最后即sub到最后则返回第一个子串在主串的位置
return buf;
}while(*bp++ == *sp++);
buf++; //如果不等,主串buf+1,子串回溯到0
}
return 0;
} int main()
{
printf("%s\n",strstr("hello world", "ell"));
return 0;
}
执行结果:
strstr和memcmp函数的实现的更多相关文章
- strcmp函数和memcmp函数的用法区别及联系
前言: C语言中有很多东西容易搞混,最近笔者就遇到了一个问题.这里做个记录.就是memcmp和strcmp两者的用法,这里做个对比: 功能对比: A memcmp: 函数原型: int memcmp( ...
- php -- strstr()字符串匹配函数(备忘)
Learn From: http://blog.csdn.net/morley_wang/article/details/7859922 strstr(string,search) strstr() ...
- strstr 函数用法
strstr 编辑 strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串.如果是,则该函数返回str2在str1中首次出现的地址:否则,返回NULL. C语言函数 编辑 ...
- <系统函数实现>memcmp
这是我实现的memcmp函数: #include <stdio.h> #include <string.h> /* *int memcmp (const void *s1,co ...
- strstr()函数的使用
strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串.如果是,则该函数返回str2在str1中首次出现的地址:否则,返回NULL. 实例: /** *Descriptio ...
- memcmp与strncmp函数【转】
c中strncmp与memcmp的区别 函数:int memcmp (const void *a1, const void *a2, size_t size) 函数memcmp用于比较字 ...
- 写出完整版的strcpy函数及其他如:strcat,strcmp,strstr的函数实现
(---牛客网中刷题---)写出完整版的strcpy函数 如果编写一个标准strcpy函数的总分值为10,下面给出几个不同得分的答案: 2分 1 2 3 4 void strcpy( char *st ...
- c语言中的部分字符串和字符函数
// // main.c // homeWork1230 // // #include <stdio.h> #include <string.h> #include <c ...
- C string 函数大全
PS:本文包含了大部分strings函数的说明,并附带举例说明.本来想自己整理一下的,发现已经有前辈整理过了,就转了过来.修改了原文一些源码的问题,主要是用char *字义字符串的问题,导致程序运行时 ...
随机推荐
- jquery 核心
1.jquery核心函数 1.1 jQuery([selector,[context]]); $("#id"),$(document.body),$(" ...
- HttpWebRequest 写入报错
HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size ...
- ntpath join(path, *paths) 发生UnicodeDecodeError的Bug的解决方案
最近在学习用Python开发web,使用的是web框架,在win8.1平台下安装SAE和Flask的时候总是在ntpath.py这个UnicodeDecodeError模块报错. 84行 result ...
- linux安装python使用的MySQLdb
安装mysqldb模块需已安装mysql 使用pip安装MySQLdb pip install mysql-python mac os安装mysqldb sudo pip install mysql- ...
- C语言学习笔记(一):数组传递时退化为指针
这几天闲来无事,写了一个数组元素排序函数如下: #include <stdio.h> #include <stdlib.h> void ArraySort(int array[ ...
- mvc4 to mvc5 and EF5 to EF6
今天把 后台的mvc 升级到了mvc5和ef6 .出错很正常. 下面是一些错误信息. [A]System.Web.WebPages.Razor.Configuration.HostSection 无法 ...
- [BZOJ 1502] [NOI2005] 月下柠檬树 【Simpson积分】
题目链接: BZOJ - 1502 题目分析 这是我做的第一道 Simpson 积分的题目.Simpson 积分是一种用 (fl + 4*fmid + fr) / 6 * (r - l) 来拟合 fl ...
- MVC自学系列之二(MVC控制器-Controllers)
Controllers的职责 1.MVC模式中的Controllers的职责是对用户的输入做出响应,对用户的输入在实体上做一些变化.它关心的是应用的流动,处理传入的数据,并给相关的View提供数据 ...
- 李洪强漫谈iOS开发[C语言-033]-三元运算符的应用
- 两次fopen不同的文件返回相同的FILE* 地址
最近接触一个垃圾程序,出现一个奇怪的bug,现象是两次fopen不同的文件返回相同的FILE*地址,第二次返回的FILE*有时候无端端的就被关闭了.以下代码是对这个bug的概括: auto fp1 = ...