Description: Given 2 string S1 and S2, we have to check if the 2 string match each other, given that one of the strings S1 contains wildcard characters.

What do we mean by ‘wildcard characters’?

‘*’ - matches to 0 or more characters

‘?’ – matches to 1 character

 
If video does not load, Click here
 
Example:

“hannah”

Matches to both

-”ha*ah”(here * matches to 2 characters “nn”)

-”Hannah”(here ? matches to character ‘n’)

Algorithm:

Consider the recursive function below to solve the problem:

/*Function to check if S1 and S2 match, given S1 may contain wildcard characters*/

Function(S1, S2, x,y)

{

If(S1[x]==‘\0’ && S2[y]==‘\0’)

return TRUE;/*end characters*/

Else If(S1[x]==‘*’ &&  S1[x+1]!=‘\0’ && S2[y]==‘\0’)

return FALSE;

Else if(S1[x]==S2[y] || S1[x]==‘?’)

return Function(S1,S2,x+1,y+1);

Else if(S1[x]==‘*’)

return (Function(S1,S2,x,y+1) || Function(S1,S2,x+1,y) );//* matches to 0 or more characters

Else

return 0;

Go to top