please help with counting specific word in a sentence
how do i count a specific word in a sentence. example:
"i have a dog"
the word "dog" occurred 1 time
here is the code i that i develop so far
Code:
public class count{
public static void main(String[] args){
String sentence = "this is a book this is a cat this is a dog";
char []abc= new char[26];
int []times=new int [26];
for(int i=0; i<abc.length;i++)
abc[i]=(char) (i+97);
for(int i=0; i<sentence.length();i++){
char c=sentence.charAt(i);
for (int j=0;j<abc.length;j++)
if (c==abc[j]) times[j]++;
}
for(int i=0;i<times.length;i++)
System.out.println(abc[i]+" was used "+times[i]+" times");
}
}
i thought that if i count each letter first then i might be able to have some idea on how to count a specific word. please help me thanks