Interview Questions

258. Implement the function bool isRegex(char *reg.....

Microsoft Interview Questions and Answers


(Continued from previous question...)

258. Implement the function bool isRegex(char *reg.....

Question:
Implement the function bool isRegex(char *reg, char *string); This function is passed two strings : a regular expression, consisting of the [a-z] and the * and ? characters. We had to check if the string matched the supplied regular expression. For example, if reg is a*b, and string is acbcb, we should return true. And if reg is a?b and string is accb, we return false..


maybe an answer:


bool IsMatch(char* p, char* s)
{
bool fMatch = false;

if (p && s)
{
switch(*p)
{
case '*':
if (!*(p+1))
{
fMatch = true;
}
else
{
while(*s)
{
if(IsMatch(p+1, s))
{
fMatch = IsMatch(p+2, s+1);
break;
}
s++;
}
}
break;
case '?':
if (!*s)
{
fMatch = false;
}
else
{
fMatch = IsMatch(p+1, s+1)

;
}
break;
default:
if (*p == *s)
{
fMatch = (!*s) ? true : IsMatch(p+1, s+1);
}
else
{
fMatch = false;
}
break;
}
}

return fMatch;
}

(Continued on next question...)

Other Interview Questions