在指定字符串中搜索字符串,不分大小写。
不要告诉我用 StrStrI()函数,调用这个函数得添加一个库#include <shlwapi.h>而我不想添加任何库,谢谢大家了啊
不要告诉我用 StrStrI()函数,调用这个函数得添加一个库#include <shlwapi.h>而我不想添加任何库,谢谢大家了啊
解决方案 »
- release正常debug出现异常中断
- 多线程Csocket中服务器返回客户端信息,只有一个得到信息?
- ddx_control有什么意义
- SyncML开发
- 谁能写出双色球排列组合的表达式?最后好像得到17721088组数据,觉得有意思想搞搞......................................................................................
- 非模态窗口的显示???
- Zip 文件的分块
- 基于对话框的应用程序中,Create无模式对话框的指针放在哪个地方删除好啊?
- dll中的对话框中,弄了个属性页。不显示。
- 实在是抄书的,怎么还是运行不了
- 服务器端没有办法进入OnClose函数?
- 使用CList时出现错误,请大侠帮忙解决,谢谢。
StringMatch.h
/* KMP.c: Implements strstr library function using the Knuth-Morris-Pratt
substring search algorithm. By Terry R. McConnell The entry point my_strstr is an implementation of the standard C library
function strstr using the KMP algorithm. Extensive documentation is
provided in the form of comments. There is also a main program invoked
as "kmp <target string>" (or with -h option for usage information) which
searches for the first occurence of the target in the stdin stream and
prints information about where it was found. Reference: Knuth, D.E., J.H. Morris, and V.R. Pratt, "Fast pattern matching
in strings", SIAM J. Computing, 6:2, 323-350. Compile: cc -o kmp kmp.c
Include -DMAX_SOURCE=... to change size of largest string that can be
searched.*//************************************************************************/
/* modified by libo, 2007.01 */
/************************************************************************/#if !defined(STRING_MATCH)
#define STRING_MATCH#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#define LOWER(x) (x >= 'A' && x <='Z' ? x + 32 : x)char *stristr(const char *src, const char *target);#endif
// MatchString.cpp#include <string.h>
#include "StringMatch.h"char *stristr(const char *src, const char *target)
{
int m, t = 0, s = 0;
unsigned int i;
int *f; m = strlen(target);
if (m == 0)
{
return (char *)src;
}
f = new int[m+1]; f[1] = 0;
for(s=1; s < m; s++)
{
while((t > 0) && (LOWER(target[s]) != LOWER(target[t])))
t = f[t]; if(LOWER(target[t]) == LOWER(target[s]))
{
f[s+1] = ++t;
}
else
{
f[s+1] = 0;
}
} for(i=0, s=0;i<strlen(src);i++)
{
while((s>0) && (LOWER(src[i]) != LOWER(target[s])))
s = f[s];
if(LOWER(src[i]) == LOWER(target[s]))
s++;
if(s == m)
{
delete []f;
return ((char *)src+i-m+1);
}
} delete []f; return NULL;
}
#define is_upper( ch ) ( (ch) >= 'A' && (ch) <= 'Z' )
#define is_letter( ch ) ( is_lower( ch ) || is_upper( ch ) )
#define upper( ch ) ( is_lower( ch ) ? (ch) : ((ch) + 'a' - 'A') )
#define is_fit( ch1, ch2 ) ( (ch1) == (ch2) || upper( ch1 ) == upper( ch2 ) )
int handler( char* tarstr, char* substr )
{
while( 1 )
{
if( *substr == '\0' )
{
return 1;
}
if( *tarstr == '\0' )
{
return 0;
}
if( !is_fit( *tarstr, *substr ) )
{
return 0;
}
tarstr ++;
substr ++;
}
return 1;
}
int find_sub_str( char tarstr[], char substr[] )
{
int tarpos = 0;
while( 1 )
{
if( tarstr[tarpos] == '\0' )
{
return -1;
} if( handler( &tarstr[tarpos], substr ) )
{
return tarpos;
} tarpos ++;
}
return tarpos;
}char* test = "faiewajfwjfwfj";
char* testsub = "eWaJ";int main()
{
printf( "%d\n", find_sub_str( test, testsub ) );
system( "PAUSE" );
return 0;
}