Wanted: string matching routine

der Mouse mouse at thunder.mcrcim.mcgill.edu
Sat Jun 29 22:43:52 AEST 1991


In article <2868E3EF.6133 at ics.uci.edu>, vahid at vesta.ics.uci.edu (Frank Vahid) writes:

> Does anyone have a routine which is similar to strcmp, but permits
> unix-type wildcard characters in at least one of the strings?  For
> example, strmatch("abc*", "abcdefg") would return a value denoting a
> successful match.

I've written such things fairly often.  If you don't particularly care
about blinding speed, it's pretty easy to do something like this
(warning: untested)

strmatch(pat,str)
char *pat;
char *str;
{
 while (1)
  { switch (*pat)
     { case '\0':
	  return(*str == '\0');
	  break;
       case '?':
	  if (*str == '\0') return(0);
	  break;
       case '*':
	  if (strmatch(pat+1,str)) return(1);
	  if (*str) str ++;
	  pat ++;
	  continue; /* skip the increments below */
	  break;
       default:
	  if (*str != *pat) return(0);
	  break;
     }
    pat ++;
    str ++;
  }
}

					der Mouse

			old: mcgill-vision!mouse
			new: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.lang.c mailing list