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: none used in this problem
- Function/Method: commonCount
- Parameters: string, string
- Returns: int
- Method signature (see below)
int numOccurrences (String s, String sub)
(be sure your method is public)
Class
public class StringCount
{
public int numOccurrences(String s, String sub)
{
// TODO: fill in numOccurrences
}
}
Constraints
- s.length() ≥ 0
- sub.length() ≤ s.length()
Examples
(quotes for clarity, they're not part of the string)
-
"apple" "s"
Returns: 0
-
"apple" "p"
Returns: 2
-
"horse" "horse"
Returns: 1
-
"mississippi" "ss"
Returns: 2
-
"bbb" "bb"
Returns: 2
JRNF
Last modified: Tue Sep 21 16:40:28 EDT 2004