fork(3) download
  1. #include <stdio.h>
  2.  
  3. unsigned matches(char const * const input, char const * const pattern) {
  4. if (! *pattern) {
  5. // An empty pattern matches only the empty
  6. // string, nothing else
  7. return (*input) ? 0 : 1;
  8. }
  9. if (*pattern == '*') {
  10. // a wildcard could be zero to any number
  11. // of characters. Sum the number of matches
  12. // for each possibility (excluding the empty
  13. // rest of input for now)
  14. char const * rest = input;
  15. unsigned count = 0;
  16. while (*rest) {
  17. count += matches(rest, pattern + 1);
  18. ++rest;
  19. }
  20. // Add matches for the empty rest of input
  21. count += matches(rest, pattern + 1);
  22. return count;
  23. }
  24. if (! *input) {
  25. // A non empty, non wildcard pattern cannot
  26. // match the empty string
  27. return 0;
  28. }
  29. if (*pattern == *input) {
  30. // non wildcard match, count the ways the rest of
  31. // the pattern matches the rest of the input.
  32. return matches(input + 1, pattern + 1);
  33. }
  34. else {
  35. // mismatch!
  36. return 0;
  37. }
  38. }
  39.  
  40. int main() {
  41. char const * const inputs[] = {
  42. "a", "aa", "xxa", "abx", "", "x", "aabb",
  43. "absolutely awesome but ..."};
  44. char const * const patterns[] = {
  45. "", "a", "*a", "a*", "*a*",
  46. "ab*", "*ab*", "**a", "*a*b*"};
  47. size_t p, i;
  48. size_t const numPatterns =
  49. sizeof(patterns)/sizeof(patterns[0]);
  50. size_t const numInputs =
  51. sizeof(inputs)/sizeof(inputs[0]);
  52. for (i = 0; i < numInputs; ++i) {
  53. printf("%s:\n", inputs[i]);
  54. for (p = 0; p < numPatterns; ++p) {
  55. printf("%s matches %u times\n",
  56. patterns[p],
  57. matches(inputs[i], patterns[p]));
  58. }
  59. }
  60. return 0;
  61. }
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
a:
 matches 0 times
a matches 1 times
*a matches 1 times
a* matches 1 times
*a* matches 1 times
ab* matches 0 times
*ab* matches 0 times
**a matches 1 times
*a*b* matches 0 times
aa:
 matches 0 times
a matches 0 times
*a matches 1 times
a* matches 1 times
*a* matches 2 times
ab* matches 0 times
*ab* matches 0 times
**a matches 2 times
*a*b* matches 0 times
xxa:
 matches 0 times
a matches 0 times
*a matches 1 times
a* matches 0 times
*a* matches 1 times
ab* matches 0 times
*ab* matches 0 times
**a matches 3 times
*a*b* matches 0 times
abx:
 matches 0 times
a matches 0 times
*a matches 0 times
a* matches 1 times
*a* matches 1 times
ab* matches 1 times
*ab* matches 1 times
**a matches 0 times
*a*b* matches 1 times
:
 matches 1 times
a matches 0 times
*a matches 0 times
a* matches 0 times
*a* matches 0 times
ab* matches 0 times
*ab* matches 0 times
**a matches 0 times
*a*b* matches 0 times
x:
 matches 0 times
a matches 0 times
*a matches 0 times
a* matches 0 times
*a* matches 0 times
ab* matches 0 times
*ab* matches 0 times
**a matches 0 times
*a*b* matches 0 times
aabb:
 matches 0 times
a matches 0 times
*a matches 0 times
a* matches 1 times
*a* matches 2 times
ab* matches 0 times
*ab* matches 1 times
**a matches 0 times
*a*b* matches 4 times
absolutely awesome but ...:
 matches 0 times
a matches 0 times
*a matches 0 times
a* matches 1 times
*a* matches 2 times
ab* matches 1 times
*ab* matches 1 times
**a matches 0 times
*a*b* matches 3 times