LazyShpee

nmatch

Dec 29th, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int     nmatch(char *s1, char *s2)
  4. {
  5.   if (*s2 == '*') // 0 or more                                                                                      
  6.     return ((*s1 ? nmatch(s1 + 1, s2) : 0) + nmatch(s1, s2 + 1));
  7.   else if (*s2 == '?') // exactly 1                                                                                  
  8.     return ((*s1 ? nmatch(s1 + 1, s2 + 1) : 0));
  9.   else if (*s2 == '+') // 1 or more                                                                                  
  10.     return ((*s1 ? nmatch(s1 + 1, s2 + 1) + nmatch(s1 + 1, s2) : 0));
  11.   if (*s2 == '-') // 0 or 1                                                                                          
  12.     return ((*s1 ? nmatch(s1 + 1, s2 + 1) : 0) + nmatch(s1, s2 + 1));
  13.   else if (*s1 == *s2) // exact match                                                                                
  14.     return ((!*s1 && !*s2) ? 1 : nmatch(s1 + 1, s2 + 1));
  15.   return (0);
  16. }
  17.  
  18. int     main(int c, char *v[])
  19. {
  20.   int m;
  21.  
  22.   if (c != 3)
  23.     return (1);
  24.   m = nmatch(v[1], v[2]);
  25.   printf("Matched : %d time%s\n", m, m > 1 ? "s" : "");
  26.   return (0);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment