TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Paul Edwards
from: Frank Adam
date: 1996-06-09 12:36:04
subject: Find a word

G'Day Paul,
 
-=> Quoting Paul Edwards to Frank Adam <=-

 PE> These malloc's are very wasteful, why don't you write your own
 PE> strstr which is case-insensitive?
Yes, they are. But i thought re-writing standard library functions 
would be a no-no, besides i didn't think of it :-)
I did have the strstr already, and now added a few tolowers to it..
Hate to step on your toes, but yours is bigger than mine :-)
Btw, perhaps you've posted the wrong one, that was case sensitive.

I've pinched your variable names, hope you don't mind, but it makes
it more readable than the way it was with mine.
                                  
/*  
** Finds the first occurrence of a string within another string
** case(Str_str()) and not case sensitive(Str_istr()).
** Public domain by Frank Adam and so on..
** Returns a pointer to the substrings position or NULL on failure.
**
*/
   
#define TEST

#ifdef TEST
 #include 
#endif
 
#include 

char* Str_str(char*,char*);
char* Str_istr(char*,char*);
size_t get_len(char*);

#ifdef TEST

main()
{
 char *s1 = "We shall fight them on the echos";
 char *s2 = "FigHt";
 char *f;
 f = Str_str(s1,s2);
 printf(f == NULL ? "\nNot Found " : "\nFound ");
 printf(f == NULL ? "%s",s2 : "%.*s",get_len(s2),f);

 f = Str_istr(s1,s2);
 printf(f == NULL ? "\nNot Found " : "\nFound ");
 printf(f == NULL ? "%s",s2 : "%.*s",get_len(s2),f); 
 return 0;
 }

#endif

char* Str_str(char *s1,char *s2)
{
 char *p = s1,*p1 = s2,*p2;
 while(*p++)
 {
  if(*p == *p1)
  {
   p2 = p;
   while(*p++ == *p1 && *p1++);
    if(!*p1) return p2;
     p = ++p2;p1 = s2;
   }
 }
 return NULL;
}

char* Str_istr(char *s1,char *s2)
{
 char *p = s1,*p1 = s2,*p2;
 while(*p++)
 {
  if(tolower(*p) == tolower(*p1))
  {
   p2 = p;
   while(tolower(*p++) == tolower(*p1) && *p1++);
    if(!*p1) return p2;
     p = ++p2;p1 = s2;
   }
 }
 return NULL;
}

size_t get_len(char *txt) /* so i wouldn't have to link in string.h */
{
 char *p = txt;int ct=0;
 while(*p++) ct++;
 return ct;
 }


  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™.