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 *字义字符串的问题,导致程序运行时 ...
随机推荐
- 查看linux进程(强制中止进程),服务及端口号,
进程状态查询 ps -aux [test@pan ~]$ ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START ...
- ubuntu14.04安装wine以及国际版QQ
公社(http://www.linuxidc.com/linux/2014-06/103768.htm)说: Wine (“Wine Is Not an Emulator” 的首字母缩写)是一个能够在 ...
- 《C和指针》章节后编程练习解答参考——6.6
<C和指针>——6.6 题目: 在指定的下限.上限之间使用数组方法查找质数,并将质数提取出来. 要求: 略 解答代码: #include <stdio.h> #define U ...
- 关于Keil的安装与注册
由于前一段时间一直在做关于stm32f407的相关内容,于是安装的Keil是MDK5,最近一阵子想再看看51单片机以前没有做过的内容,就要再安装一个Keil C51,结果就不可避免的遇到了两个软件必须 ...
- Meta 的两个 相关属性
Meta标签中的apple-mobile-web-app-status-bar-style属性及含义: “apple-mobile-web-app-status-bar-style”作用是控制状态栏显 ...
- 写个简单的ANT脚本来编译项目
<?xml version="1.0" encoding="GBK"?> <project name="j2ee project&q ...
- Warm up 2
hdu4619:http://acm.hdu.edu.cn/showproblem.php?pid=4619 题意:题目大意:给你两种纸牌 ,一种水平放置共有n张 ,一种竖直放置共有m张.水平放置的纸 ...
- Feel Good
poj2796:http://poj.org/problem?id=2796 题意:给出一个长度为n(n<100000)的序列,求出一个子序列,使得这个序列中的最小值乘以这个序列的和的值最大. ...
- LeetCode解题报告:Reorder List
Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Yo ...
- 如何对一个不断更新的HashMap进行排序
如何对一个不断更新的HashMap进行排序? 解答:等到HashMap更新稳定后,用ArrayList包装进行排序.或者自己写一个可以类似HashMap的有序Map,每次更新的时候都进行排序,构建自己 ...