C program to check if two strings match where one string contains wild characters

Checking if two strings match where one string contains wildcard characters

Wild Card characters are characters like ‘ * ‘ and ‘ ? ‘. In this C program we will be checking that if two strings match where one string contains wildcard characters. We will be using the concept of ternary operator and pointers in this program, to increase the efficiency and reduce the complexity of the program. For better understanding of this program, you should atleast be having a good knowledge of ternary operator and pointers

C program to check if twostrings match where one string contains wildcard characters
Competitive Coding Techniques

Algorithm

  • Start
  • Accept the inputs
  • check if both the strings are of same length
  • comparing each character of both the strings, along with the wild characters
  • printing the output
    • yes, if both the strings are same
    • no, if both the strings are different

C program to check if two strings are same, if one string contains wild characters

#include  <stdio.h> 
#include  <stdbool.h>
bool check(char *str1, char * str2) ;// declaration of the check() function
int main() 
{ 
    char str1[100],str2[100];
    printf("Enter first string with wild characters : ");
    gets(str1);
    printf("Enter second string without wild characters : ");
    gets(str2);
    test(str1,str2);
    return 0; 
} 

bool check(char *str1, char * str2) 
{ 
    // checking end of both the strings 
    if (*str1 == '\0' && *str2 == '\0') 
         return true; 
  
    // comparing the characters of both the strings and wild characters(*)
    if (*str1 == '*' && *(str1+1) != '\0' && *str2 == '\0') 
         return false; 
  
    // checking wild characters(?) 
    if (*str1 == '?' || *str1 == *str2) 
         return check(str1+1, str2+1); 
  
    
    if (*str1 == '*') 
         return check(str1+1, str2) || check(str1, str2+1); 
     return false; 
} 
  
// test() function for running test cases
void test(char *str1, char *str2) 
{ 
    check(str1, str2)? puts(" Yes "): puts(" No "); 
    
}
Output
Enter the first string with wild characters : prep***ta
Enter second string without wild characters : prepinsta
yes