count occurrences of word in sentence in java code example
Example: check how many of a word is in a string
function countOccurences(str,word)
{
// split the string by spaces in a
String a[] = str.split(",");
// search for pattern in a
int count = 0;
for (int i = 0; i < a.length; i++)
{
// if match found increase count
if (word.equals(a[i]))
count++;
}
return count;
}