TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: All
from: Frank Adam
date: 1996-06-07 18:30:00
subject: Find a word

/*
** char* Find_Word(char *buffer,char *word, int mode)
** char* In_Str(char *buffer,char *word)
** Public domain by Frank Adam, and so on...
**
** In_Str is a case insensitive strstr(), used by Find_Word.
**
** Find_Word finds *whole* words ONLY, case or non sensitive, in strings.
** If mode = 0 search is case sensitive, otherwise non sensitive(rude?)
** Returns a pointer to the first character if found, NULL otherwise.
** strstr() stole this idea from me, truely.:-)
** Bug reports,praises,money,girls, etc are welcome.
*/

#define TEST

#include
#include
#include

#ifdef TEST
 #include
#endif

/*
** Below are defines of some chars sometimes found on the left and right
** side of words, yet not being part of that word. Others could be added
** some omitted, but the ' '(ASCII 32) char is required.
*/                                                                     

#define LEFTCHARS  "\r\n\t-: "
#define RIGHTCHARS ");:.!,'?\r\n\t "


char* Find_Word(char *txt,char *wrd,int);
char* In_Str(char* ,char*);

#ifdef TEST

main()
{
 char txt[] = "Hello \rworld\n here: i \ncome!";
 char *ptr;int i;
 char *findme[] =
{"HELLO","woRLd","here","i","wontfindme","come"};

 printf("\nCase sensitive test:");
  for( i = 0;i<6;i++)
  {
   ptr = Find_Word(txt,findme[i],0);
   if(ptr) printf("\n%.*s",strlen(findme[i]),ptr);
    else printf("\nNot found \"%s\" !",findme[i]);
   }

 printf("\n\nNon case sensitive test:");
  for( i = 0;i<6;i++)
  {
   ptr = Find_Word(txt,findme[i],1);
   if(ptr) printf("\n%.*s",strlen(findme[i]),ptr);
    else printf("\nNot found \"%s\" !",findme[i]);
   }

return 0;
}

#endif

char* Find_Word(char *buf,char *wrd,int mode)
{
 char *ptr = buf,*ptr1,*ptr2;

  if(mode == 0) ptr1 = strstr(ptr,wrd);
  else ptr1 = In_Str(buf,wrd);

  if(NULL == ptr1) return NULL;
  if(ptr1 == buf)
  {
   ptr2 = ptr1 + strlen(wrd);
   if(strchr(RIGHTCHARS,*ptr2) != NULL) return ptr1;
   }
  else if(strchr(LEFTCHARS,*--ptr1) != NULL)
  {
   ptr2 = ++ptr1 + strlen(wrd);
   if(strchr(RIGHTCHARS,*ptr2) != NULL) return ptr1;
   }
return NULL;
}

char* In_Str(char *buf,char *wrd)  /* case insensitive strstr() */
{
 char *txt;char *pat;char *ptr;
 txt = (char*) malloc(strlen(buf)+1);
 pat = (char*) malloc(strlen(wrd)+1);
 if(!txt || !pat) return NULL;
 strcpy(txt,buf); strcpy(pat,wrd);     /* leave the originals alone */
 ptr = txt;while(*ptr) *ptr++ = tolower(*ptr);
 ptr = pat;while(*ptr) *ptr++ = tolower(*ptr);
 ptr = strstr(txt,pat);
 if(ptr) ptr = buf+(ptr-txt);
 free(txt);free(pat);
 return ptr;
}

  L8r Frank(fadam{at}ozemail.com.au)
  
___ Blue Wave/DOS v2.21

--- Maximus 3.01
* Origin: The Software Parlour (3:635/544)
SEEN-BY: 50/99 620/243 623/630 632/349 635/503 544 727 711/401 409 410 413
SEEN-BY: 711/430 808 809 932 934 712/515 713/888 714/906 800/1
@PATH: 635/544 50/99 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.