Wildcard Pattern Matching

For this problem, one main string and another wildcard patterns are given. In this algorithm, it will check whether the wildcard pattern is matching with the main text or not.

The wildcard pattern may contain letters or ‘*’ or ‘?’ Symbols. The ‘?’ Is used to match a single character and ‘*’ is used to match the sequence of characters including empty space.

When the character is ‘*’: We can ignore the star character and move to check next characters in the pattern.

When the next character is ‘?’, then we can ignore only the current character in the text, and check for the next character in pattern and text.

When the pattern character is other than ‘*’ and ‘?’, then if the current character of pattern and text is matching, then only move further.

Input and Output

Input:
The main string and the wildcard pattern.
Main String “Algorithm”
Pattern “A*it?m”
Output:
The pattern matched.

Algorithm

wildcardMatch(text, pattern)

Input: The main text and the pattern.

Output: True when wildcard patterns matched for the main text.

Begin
   n := length of the text
   m := length of pattern

   if m = 0, then
      return 0 if n = 0, otherwise return 1
   i := 0, j := 0

   while i 

Example

#include
using namespace std;

bool wildcardMatch(string text, string pattern) {
   int n = text.size();
   int m = pattern.size();

   if (m == 0)    //when pattern is empty
      return (n == 0);

   int i = 0, j = 0, textPointer = -1, pattPointer = -1;
   while (i > text;
   cout > pattern;
   
   if (wildcardMatch(text, pattern))
      cout 

Output

Enter Text: Algorithm
Enter wildcard pattern: A*it?m
Pattern Matched.
Updated on: 2020-06-17T07:27:53+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements