title: Return the number of times query occurs as a substring of src
body: <pre><code>/** Return the number of times query occurs as a substring of src
   * (different occurrences may overlap).
   * Precondition: query is not the empty string "".
   * Examples: For src = "ab", query = "b", return 1.
   *           For src = "Luke Skywalker", query = "ke", return 2.
   *           For src = "abababab", query = "aba", return 3.
   *           For src = "aaaa", query = "aa", return 3.*/
  public static int numOccurrences(String src, String query) {
      /* This should be done with one loop. If at all possible, don't have
       * each iteration of the loop process one character of src. Instead,
       * see whether some method of class String can be used to jump from one
       * occurrence of query to the next. */
      int count = 0;
      for (int i = 0; i &lt; src.length(); i++) {
          int end = i + query.length() - 1;
          if (end &lt; src.length()) {
              String sub = src.substring(i, end);
              if (query.contentEquals(sub)) 
                  ++count;
          } 
      }return count;
  }
  </code></pre>
  
  <p>I tested the code. If the src is "cherry" and the query is "err", then the output is expected to be 1 but it turns out to be 0. What's wrong with the code? BTW, I cannot use methods outside the String class.</p>