Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int nmatch(char *s1, char *s2)
- {
- if (*s2 == '*') // 0 or more
- return ((*s1 ? nmatch(s1 + 1, s2) : 0) + nmatch(s1, s2 + 1));
- else if (*s2 == '?') // exactly 1
- return ((*s1 ? nmatch(s1 + 1, s2 + 1) : 0));
- else if (*s2 == '+') // 1 or more
- return ((*s1 ? nmatch(s1 + 1, s2 + 1) + nmatch(s1 + 1, s2) : 0));
- if (*s2 == '-') // 0 or 1
- return ((*s1 ? nmatch(s1 + 1, s2 + 1) : 0) + nmatch(s1, s2 + 1));
- else if (*s1 == *s2) // exact match
- return ((!*s1 && !*s2) ? 1 : nmatch(s1 + 1, s2 + 1));
- return (0);
- }
- int main(int c, char *v[])
- {
- int m;
- if (c != 3)
- return (1);
- m = nmatch(v[1], v[2]);
- printf("Matched : %d time%s\n", m, m > 1 ? "s" : "");
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment