HI, so my code compiles but does not give the result I intended. Any help is greatly appreciated!
How can we count the number of occurrences of a character or group of characters (a substring) inside of a given string of characters? In other words, how can we count the number of time a certain section of text appears in another section of text?
Your assignment is to write a SubstringCounter class with only one method:
public static int substringCounter(String string, String substring)
The method will utilize two strings as passed parameters. The first string is the master string to be searched, and the second string is the string being searched for. The method returns the count (an integer) of the number of times the substring is found in the master string. You should assume that either the master string or the substring is one or more characters, and that either one may be longer than the other. The substring (obviously) may occur one or more times in the master string, or possibly not at all. You will want to have some kind of process that will look at each of the characters in the master string and count every time the substring is found.
If you are not sure exactly how you want to do this, you will want to review the advanced operations on strings section of the textbook, and in particular the string method descriptions listed on pages 265-6. You will also want to look at the IndexOfExample and SubstringExample classes that are provided for you in the project download.
To get started, download, extract, and save the Substring Counter project to your computer. Open up the project by clicking on the BlueJ package file icon. You can run the SubstringCounterTest class anytime you like to see how much of the assignment you have completed (go ahead, and give it a try right now). When you have written your method, run the tester and see if your code passes the 10 rigorous tests.
public class SubstringCounter
{
public static int substringCounter(String master, String substring)
{ int count = 0 ;
for(int pos = 0 ; pos < master.length() ; pos++)
{
int index = master.indexOf(substring, pos) ;
if(index!=0){
pos = count ;
count++ ;
}
}
return count ;
}
}
This is what the tester says:
Now testing your SubstringCounter class:
Found method substringCounter(String string, String substring)
Failed: substring 'the' does not appear 178 times in the string
'Fourscore and seven years ago, our fathers brought forth upon this continent
a new nation, conceived in liberty and dedicated to the proposition
that all men are created equal.'
Failed: substring 'and' does not appear 178 times in the string
'Fourscore and seven years ago, our fathers brought forth upon this continent
a new nation, conceived in liberty and dedicated to the proposition
that all men are created equal.'
Failed: substring 'e' does not appear 178 times in the string
'Fourscore and seven years ago, our fathers brought forth upon this continent
a new nation, conceived in liberty and dedicated to the proposition
that all men are created equal.'
Failed: substring ' ' does not appear 178 times in the string
'Fourscore and seven years ago, our fathers brought forth upon this continent
a new nation, conceived in liberty and dedicated to the proposition
that all men are created equal.'
Failed: substring '.' does not appear 178 times in the string
'Fourscore and seven years ago, our fathers brought forth upon this continent
a new nation, conceived in liberty and dedicated to the proposition
that all men are created equal.'
Failed: substring 'F' does not appear 178 times in the string
'Fourscore and seven years ago, our fathers brought forth upon this continent
a new nation, conceived in liberty and dedicated to the proposition
that all men are created equal.'
Failed: substring 'aa' does not appear 5 times in the string
'aaaaa'
Failed: substring 'googoo' does not appear 18 times in the string
'googoogoogoogoogoo'
Failed: substring 'beanwhip' does not appear 12 times in the string
'doobydoobydo'
Failed: substring 'biggerstring' does not appear 6 times in the string
'string'
Bummer, try again