Hi,
I'm writing this code where I want to match a string with a signature.
A wildcard '?' would mean it can skip 1 character. A wildcard '*' means it can skip 0 to 32 characters.
For example, say I have:
string "screwed" and a signature "screw?d"
These two would match.
Say I have:
string "screw33333d" and a signature "screw*d"
These two would match also.

The code I have right now looks like this, and it seems to work fine with the '*' wildcard, but I don't understand why '?' is not working.

int i, star;
	new_segment: 
	star = 0; 
	while (sig[0] == '*') 
	{ 
		star = 1; 
		sig++; 
	} 
	test_match:
	for (i = 0; sig[i] && (sig[i] != '*'); i++)
	{
      if (sig[i] != text[i])
	  {
         if (! text[i]) return false;
         if (sig[i] == '?') continue;
         if (! star) return false;
         text++;
         goto test_match;
	  }
   }
   if (sig[i] == '*')
   {
      text += i;
      sig += i;
      goto new_segment;
   }
   if (! text[i]) return true;
   if (i && sig[i-1] == '*') return true;
   if (! star) return false;
   text++;
   goto test_match;

Can anyone help me?

>> if (sig != text)
You can't use the same counter here because sig and text can be of different lengths.

If sig == "screeeeewxd" and text == "scr*w?d" then I would do this:

int main()
{
    char* text = "scr*w?d";
    char* sig = "screeeewxd";
    int i = 0;
    int j = 0;
    for(i = j = 0; text[i] && sig[j]; ++i )
    {
        if( text[i] == '*')
        {
            while( sig[j] && (sig[j] != text[i+1]) )
                j++;
        }
        else if( sig[j] == text[i] || text[i] == '?' )
        {
            j++;
        }
    }

}
Member Avatar for jencas

This algorithm does not work for i.e. screeeewywxd

it was almost there

int main()
{
    char* text = "scr*w?d";
    char* sig = "screeeewywxd";
    int i = 0;
    int j = 0;
    for(i = j = 0; text[i] && sig[j]; ++i )
    {
        if( text[i] == '*')
        {
            while( sig[j] && (sig[j] != text[i+1]) )
                j++;
        }
        else if( sig[j] == text[i] || text[i] == '?' )
        {
            j++;
        }
        else
        {
            printf("Match failed\n");
            break;
        }
    }

}

Ok, I see... However, I also need the case if I have a string "I am screeeewed", it should still match "scr*w?d", because "I am" will be ignored while "screeeewed" will be a matching string.
In other words, how can I change the code so that it can also analyze 'parts' of the text i want to compare?

You don't have to change a thing, just pass "I am scr*w?d". I don't think this function should be required to find screeeewed in a larger sentence.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.