public static void findPatterns(String haystack, String[] patterns)



Prints the number of matches between a pattern and the words in a string (“haystack”).  Words in the haystack string are delimited by the following characters:

·         space                   (ASCII 0x20)

·         double quotation mark   (ASCII 0x22)

·         single quotation mark   (ASCII 0x27)

·         backspace               (ASCII 0x08)

·         horizontal tab          (ASCII 0x09)

·         new line                (ASCII 0x0A)

·         carriage return         (ASCII 0x0D)

·         form feed               (ASCII 0x0F)


Only exact matches are counted:

-   Substring matches are not counted

-   Matching is case sensitive 

Searches for 0 to 5 patterns.  Prints an error message and exits if the passed string array contains more than 5 elements.



haystack – The string to be searched.

patterns – An array of 0 to 5 patterns to search for. 




String haystack_parm = "To be or not to be: that is the question.";

String[] patterns_parm = {"to", "be", "be:", "or not"};

findPatterns(haystack_parm, patterns_parm);






or not:0





Notes to developer:


findPatterns is a nice method with intuitive variable names that make the code easy to read.  Way to go!  I have a couple of comments that are motivated by my empathy for potential users of the method.

1.      The error message is more helpful if it provides specific information about what is wrong.  Please consider this change:

if (patterns.length > 5)

{System.err.println("You can search for a maximum of 5 patterns.  You searched for too many patterns: " + patterns.length );}


2.  The behavior of the method is confusing because the criteria for a valid pattern is not the same as the criteria for a word that is split from the haystack string.

For example, the method allows a pattern that contains interior blank space but does not allow a haystack word that contains interior blank space.  The result is that a valid pattern can exist in the haystack string but is not counted.


Consider the example where the haystack string is “To be or not to be” and a pattern is “To be”.  I would expect the count to be 1, but the output prints 0 occurrences:

To be:0

Please consider two options for a behavior that is more intuitive to me 😊 and perhaps to other users:

A.      Restrict the patterns in the same way that words in the haystack are split.  Issue a warning that a pattern with an interior blank space like “To be” is not valid.  Do not print a count of the invalid pattern occurrences.  A pattern that contains any of the haystack split characters ([ \"\'\t\n\b\f\r]) is not valid.

B.      Modify the matching algorithm so that a pattern like “To be” finds a match in a string like “To be or not to be”.


Please let me know if you have any questions, and thanks for considering.