code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

/

/* +++Date last modified: 05-Jul-1997 */

/*

** Designation: StriStr

**

** Call syntax: char *stristr(char *String, char *Pattern)

**

** Description: this function is an ANSI version of strstr() with

**case insensitivity.

**

** return item: char *pointer if Pattern is found in String, else

**pointer to 0

**

** Rev History: 07/04/95 Bob Stout ANSI-fy

**02/03/94 Fred Cole Original

**

** Hereby donated to public domain.

*/

#include 

#include 

#include 

#include "snip_str.h"

typedef unsigned int uint;

#if defined(__cplusplus) && __cplusplus

    extern "C" {

    #endif

    char *stristr(const char *String, const char *Pattern)

        {

        char *pptr, *sptr, *start;

        uint slen, plen;

        for (start = (char *)String,

        pptr = (char *)Pattern,

        slen = strlen(String),

        plen = strlen(Pattern);

        /* while string length not shorter than pattern length */

        slen >= plen;

        start++, slen--)

            {

            /* find start of pattern in string */

            while (toupper(*start) != toupper(*Pattern))

                {

                start++;

                slen--;

                /* if pattern longer than string */

                if (slen < plen)

                return(NULL);

            }

            sptr = start;

            pptr = (char *)Pattern;

            while (toupper(*sptr) == toupper(*pptr))

                {

                sptr++;

                pptr++;

                /* if end of pattern then pattern was found */

                if ('\0' == *pptr)

                return (start);

            }

        }

        return(NULL);

    }

    #if defined(__cplusplus) && __cplusplus

}

#endif

#ifdef TEST

int main(void)

    {

    char buffer[80] = "heLLo, HELLO, hello, hELLo, HellO";

    char *sptr = buffer;

    while (0 != (sptr = stristr(sptr, "hello")))

    printf("Found %5.5s!\n", sptr++);

    return(0);

}

#endif /* TEST */

 

 

Other 156 submission(s) by this author