c语言字符串内容匹配,C语言字符串匹配函数

2023-05-16

C语言字符串匹配函数,保存有需要时可以用:

#include

#include

#include

#include

#include

/*

pattern:

pos:

*/

static int badShift[];

static int goodPostfixLastPos(const char *pattern,int pos)

{

#define _break(flag) if(flag){ break;}

int flag = ;

int len = strlen(pattern);

int postFix_len = len - pos;

int postFix_position = pos;

int initStart = pos - postFix_len;

int last_start = ;

while(postFix_len)

{

last_start = (postFix_position == pos) ?initStart:;

int postFix_start = postFix_position;

for(;last_start>= && postFix_start

{

flag = (pattern[last_start] == pattern[postFix_start]);

_break(!flag);

}

_break(flag);

if(initStart >= )

{

initStart--;

}

else

{

postFix_position++;

postFix_len--;

}

}

return flag?last_start-:-;

}

static int *calc_goodPostfixShift(const char *pattern,int *goodShift)

{

int len = strlen(pattern);

for(int i=;i

{

goodShift[i] = len - goodPostfixLastPos(pattern,i) - ;

}

return goodShift;

}

static int *clac_badcharShift(const char *ptrn)

{

int i;

int pLen = strlen(ptrn);

for(i = ; i < ; i++)

{

*(badShift+i) = pLen;

}

while(pLen != )

{

*(badShift+(unsigned char)*ptrn++) = --pLen;

}

return badShift;

}

int BMSearch(const char *str,const char *pattern)

{

int goodShift[strlen(pattern)];

int len1 = strlen(str);

int len2 = strlen(pattern);

clac_badcharShift(pattern);

calc_goodPostfixShift(pattern,goodShift);

for(int i=len2 - ;i

{

int start = i;

int pos_pattern = len2 - ;

for(;pos_pattern>=;pos_pattern--,start--)

{

if(str[start] != pattern[pos_pattern])

{

break;

}

}

if(pos_pattern < )

{

return start + ;

}

if(pos_pattern == (len2 - ))

{

i += badShift[str[start]];

}

else

{

i += goodShift[pos_pattern + ];

}

}

return -;

}

【C&plus;&plus;实现python字符串函数库】二:字符串匹配函数startswith与endswith

[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

python实现 字符串匹配函数

通配符是 shell 命令中的重要功能,? 表示匹配任意 1 个字符,*表示匹配 0 个或多个字符.请使用你熟悉的编程语言实现一个字符串匹配函数,支持 ? 和 * 通配符.如 "a?cd*d ...

c语言字符处理函数常见使用集合

1.最近看一些开源项目代码时,总会看到 c 语言中一些  "str" 开头的处理字符串的用法,有的之前没用到过,特此记录,随时看到随时添加. 这里不提出源码,只是一些使用说明加例子 ...

php -- strstr&lpar;&rpar;字符串匹配函数(备忘)

Learn From: http://blog.csdn.net/morley_wang/article/details/7859922 strstr(string,search) strstr() ...

总结C语言字符检测函数:isalnum、isalpha&period;&period;&period;

前言:最近一直在刷leetcode的题,用到isalnum函数,用man手册查找了一下,总共有13个相关函数如下: #include int isalnum(int c ...

C语言-字符操作函数

1字符数组的初始化: 1.1 char string={'c','h','i','n','a'} 1.2char string={"china"}或者去掉{}即char strin ...

Python中字符串匹配函数startswith&lpar;&rpar;函数

1.函数用途含义 Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在指定范围内 ...

函数:MySQL中字符串匹配函数LOCATE和POSITION使用方法

1. 用法一 LOCATE(substr,str) POSITION(substr IN str) 函数返回子串substr在字符串str中第一次出现的位置.如果子串substr在str中不存在,返回 ...

C语言字符串匹配、goto语句、关机命令使用

1.程序执行修改窗口字体颜色命令: 2.程序执行修改窗口标题命令: 3.程序执行关机倒计时命令: 4.根据提示输入团队名称JYHACK TEAM 根据提示输入团队网址:http://bbs.jyhac ...

随机推荐

在 &period;NET 中开发基于 Chrome 内核的浏览器-创建一个简单浏览器

首先在 http://www.cftea.com/tools/downloads/Cef.zip 下载文件包. 一.将文件解压拖入到 Visual Studio 对应的 WinForm 项目中. 二. ...

Linux 基础命令

man      帮助命令   命令 --help   命令的简单帮助 help      命令的帮助(bash的内置命令) mkdir    创建目录  如makdir /data mkdir   ...

topcoder SRM 628 DIV2 BracketExpressions

先用dfs搜索所有的情况,然后判断每种情况是不是括号匹配 #include #include #include # ...

解决SharePoint文档库文件在搜索结果页面显示的标题和文档的标题不一致问题(search result)

问题表现: SharePoint 2013 爬网后,搜索一个文档,虽然搜到了,但是显示有点问题,如图: 原因分析: 造成该问题的原因是,该文档除了本身有一个名称外,在文档metadata的title属 ...

mybatis-spring

现成的中文文档 首先,项目依赖 org.mybatis.spring.boot

SpringMVC深入探究&lpar;1&rpar;——DispatcherServlet与初始化主线

在上一篇文章中,我们给出了构成SpringMVC应用程序的三要素以及三要素的设计过程.让我们来归纳一下整个设计过程中的一些要点: SpringMVC将Http处理流程抽象为一个又一个处理单元 Spri ...

第二百二十八天 how can I 坚持

hibernate 还有好多不会搞啊,本来很简单的东西,没用过就不会. 今天... 只是感觉很累,昨天爬山爬的,不知道该写点啥了,买的羽绒服到了,还行吧,凑合穿吧. 睡觉了.今天貌似又发脾气了.哎.. ...

JavaScript状态模式及状态机模型

这是一篇,我自己都看不完的文章... 文章大体就两部分: 状态模式的介绍 状态机模型的函数库javascript-state-machine的用法和源码解析 场景及问题背景: 我们平时开发时本质上就是 ...

高级OPENGL&comma; 利用uniform块接口

1.找到需要的uniform块的索引, 将程序对象的该uniform块索引绑定uniform 缓冲对象的绑定点 2.建立uniform缓冲对象,对象绑定GL_UNIFORM_BUFFER缓冲目标,为缓 ...

什么时候删除指针后,要给指针赋NULL

删除后需要赋NULL: 1.当在一个类里的时候,删除类的某个成员对象,需要给它赋NULL,以防其他地方使用这个成员的时候,不知道这个成员是否存在 eg: ref1::ref1() { tPint = ...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c语言字符串内容匹配,C语言字符串匹配函数 的相关文章

随机推荐