Thread: wildcard system

  1. #1
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455

    wildcard system

    i need help making a wildcarding system

    so far ive checked if hters a wildcard in the front, and the back. thats all i want for now
    then after that, i dont know. lol. i guess if theres a wildcard in the front, get the last word (or bunch), if theres one in the back, compare the first word (or bunch), and on both sides just find the string in it

    whats confusing me is:
    if i have
    *hello
    and i type in
    i say hello

    do i just extract everything from the * to the end, and then, reverse the string i say (i say hello, reverse that), and compare it backwards untill the pattern string is done?

    any other ideas?
    Last edited by the Wookie; 01-13-2003 at 02:15 PM.

  2. #2
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    ok heres my code, works for the most part i guess

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    int main(int argc, char *argv[]){
    	char *pattern = new char[255];
    	char *patcpy = new char[255];
    	char *patcheck = new char[255];
    	
    	//wildcard locations
    	bool wcfront=false;
    	bool wcback=false;
    	bool found=true;
    
    	strcpy(pattern,"");
    	strcpy(patcheck,"");
    
    	printf("Enter pattern (eg, *hello): ");
    	gets(pattern);
    	printf("Enter string to check pattern against: ");
    	gets(patcheck);
    
    	//get wildcard in front 
    	//strcpy(patcpy,pattern);
    	while(strlen(pattern)){
    		if(*pattern == '*'){
    			*pattern++;
    			wcfront=true;
    			break;
    		}
    		else if(*pattern!=' ' && *pattern!='*'){
    			break;
    		}
    		else{
    			*pattern++;
    		}
    	}
    
    	//wildcard in back, strip it off
    	pattern=strrev(pattern);
    	while(strlen(pattern)){
    		if(*pattern=='*'){
    			*pattern++;
    			wcback=true;
    			break;
    		}
    		else if(*pattern!=' ' && *pattern!='*'){
    			break;
    		}
    		else {
    			*pattern++;
    		}
    	}
    	pattern=strrev(pattern);
    
    	if(wcfront && wcback){
    		if(strncmp(pattern,patcheck,strlen(pattern))!=0){
    			printf("Pattern found in front and back");
    		}
    	}
    	else if(wcfront){
    		patcheck = strrev(patcheck);
    		pattern = strrev(pattern);
    		while(strlen(pattern) && strlen(patcheck)){
    			if(*patcheck != *pattern){
    				found=false;
    				break;
    			}
    			*pattern++;
    			*patcheck++;
    		}
    
    		if(found){
    			printf("Pattern found in front");
    		}
    	}
    	else if(wcback){
    		while(strlen(pattern) && strlen(patcheck)){
    			if((*patcheck != *pattern)){
    				found=false;
    				break;
    			}
    			*pattern++;
    			*patcheck++;
    		}
    
    		if(found){
    			printf("Pattern found in back");
    		}
    	}
    	else {
    		if(!strcmp(pattern,patcheck)){
    			printf("They are equal");
    		}
    		else {
    			printf("No match");
    		}
    	}
    
    	int blah=getch();
    		
    	return 1;
    }

    i tested it a bunch, anyone found any errors or things i can do to make it more effecient?

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Here's a function:
    Code:
    bool match(const char *mask, const char *s)
    {
      const char     *sNote;          
      const char     *mNote = 0;                                    
      for (;;)
      {
        if (! *s)
        {
          while (*mask == '*')
            ++mask;
          return ! *mask;
        }
        if (*mask == '*')
        {
          if (! *++mask)
            return true
          mNote = mask;
          sNote = s + 1;
          continue;
        }
        if (*mask == *s || *mask == '?')
        {
          ++mask, ++s;
          continue;
        }
        if (! mNote)
        	return false;
        mask = mNote;
        s = sNote++;       
      }
    }
    it returns true if the mask (with * and ?) matches the string.

    BTW, do you want a function for approximate matching too? I wrote one the other day.
    Last edited by Sang-drax; 01-13-2003 at 04:13 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    wow thats super short..lol..trying to understand it. mask is the pattern and s is the string itself?

    also, whats the ? for

    and yeah, sure, thanks
    Last edited by the Wookie; 01-13-2003 at 08:54 PM.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    The ? matches any character.

    "pe???r" matches "petter" but not "peter"
    "pet*r" matches both
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    oic, gotcha thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 01:04 PM
  2. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 07:56 PM
  3. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  4. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 05:35 PM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM