numOccurences APT


Problem Statement

A common task in string manipulation is to count the number of times a substring occurs in another string.

Write a function that takes two strings and returns the number of times that string sub appears in string s.

Tip: Make sure not to run off the end of the string.

Definition

Class

public class StringCount { public int numOccurrences(String s, String sub) { // TODO: fill in numOccurrences } }

Constraints

Examples

(quotes for clarity, they're not part of the string)
  1.   "apple" "s"
    
    Returns:  0
    

  2.     
      "apple" "p"
    
    Returns:  2
    

  3.   "horse" "horse"
    
    Returns:  1
    

  4.   "mississippi" "ss"
    
    Returns:  2
    
  5.   "bbb" "bb"
    
    Returns:  2
    

JRNF
Last modified: Tue Sep 21 16:40:28 EDT 2004