在指定字符串中搜索字符串,不分大小写。
不要告诉我用 StrStrI()函数,调用这个函数得添加一个库#include <shlwapi.h>而我不想添加任何库,谢谢大家了啊

解决方案 »

  1.   

    正则表达式  hoho不加库就自己写搜索算法
      

  2.   

    我这里有一个,是用KMP实现的,是我用牛人的代码改的,共享一下。头文件:
    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;
    }
      

  3.   

    不加任何库,就不要用C++,C/C++就是用函数库堆起来的。API更是如此。
      

  4.   

    写了一个,没有怎么测试#include <stdio.h>#define is_lower( ch )       ( (ch) >= 'a' && (ch) <= 'z' )
    #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;
    }