Limited access

Upgrade to access all content for this subject

Which of the following methods will accept an original string and a search string as parameters, and return the count of how many times the search string is found in the original string?

For example:

countString("mississippi", "i") will return 4
countString("mississippi", "is") will return 2
countString("peanuts", "to") will return 0

I.

public static int countString(String str, String search)    
{
    int count = 0;

    for (int i=0; i<str.length(); i++)
    {
        if (str.substring(i, i+1).equals(search))
            count++;
    }
    return count;
}

II.

public static int countString(String str, String search)
{
    int count = 0;

    for (int i=0; i<str.length()-search.length()+1; i++)
    {
        if (str.substring(i,i+search.length()).equals(search))
            count++;
    }
    return count;
}

III.

public static int countString(String str, String search)
{
    int count = 0;

    for (int i=0; i<str.length(); i++)
    {
        if (str.indexOf(search)>=0)
             count++;
    }
    return count;
}
A

I only.

B

II only.

C

III only.

D

I and II only.

E

II and III only.

Select an assignment template