stristr.c

来自「国外网站上的一些精典的C程序」· C语言 代码 · 共 86 行

C
86 行
字号
/*** 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:  16/07/97  Greg Thayer  Optimized**               07/04/95  Bob Stout    ANSI-fy**               02/03/94  Fred Cole    Original**               09/01/03  Bob Stout    Bug fix (lines 40-41) per Fred Bulback**** Hereby donated to public domain.*/#include <stdio.h>#include <string.h>#include <ctype.h>#include "snip_str.h"typedef unsigned int uint;#if defined(__cplusplus) && __cplusplus extern "C" {#endifchar *stristr(const char *String, const char *Pattern){      char *pptr, *sptr, *start;      for (start = (char *)String; *start != NUL; start++)      {            /* find start of pattern in string */            for ( ; ((*start!=NUL) && (toupper(*start) != toupper(*Pattern))); start++)                  ;            if (NUL == *start)                  return NULL;            pptr = (char *)Pattern;            sptr = (char *)start;            while (toupper(*sptr) == toupper(*pptr))            {                  sptr++;                  pptr++;                  /* if end of pattern then pattern was found */                  if (NUL == *pptr)                        return (start);            }      }      return NULL;}#if defined(__cplusplus) && __cplusplus }#endif#ifdef TESTint main(void){      int i;      char *buffer[2] = {"heLLo, HELLO, hello, hELLo, HellO", "Hell"};      char *sptr;      for (i = 0; i < 2; ++i)      {            printf("\nTest string=\"%s\"\n", sptr = buffer[i]);            while (0 != (sptr = stristr(sptr, "hello")))            {                  printf("Testing %s:\n", sptr);                  printf("Found %5.5s!\n", sptr++);            }      }      return(0);}#endif /* TEST */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?