I have written C code that takes an input str and returns result matching against pattern.
The code does not return the proper result in certain scenarios.
For example, if the input string is "pat.com/xyzabc"
and pattern is "pat.*/*abc*"
, the program does not return a match.
Similarly it does not match pattern "pat.*/*abc"
or "pat.com/*abc*"
.
Please help how can I correct the following code?
int strmatch(const char str[], const char pattern[],
int n, int m)
{
// empty string
if (m == 0)
return (n == 0);
// initailze lookup table to false
int lookup[n + 1][m + 1];
memset(lookup, 0, sizeof(lookup));
// empty pattern can match with empty string
lookup[0][0] = 1;
// Only '*' can match with empty string
for (int j = 1; j <= m; j++)
if (pattern[j - 1] == '*')
lookup[0][j] = lookup[0][j - 1];
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (pattern[j - 1] == '*' || pattern[j - 1] == '*')
lookup[i][j] = lookup[i][j - 1] ||
lookup[i - 1][j];
else if (str[i - 1] == pattern[j - 1])
lookup[i][j] = lookup[i - 1][j - 1];
// If characters don't match
else lookup[i][j] = 0;
}
}
return lookup[n][m];
}
Aucun commentaire:
Enregistrer un commentaire