i was trying to write a code for counting the number of ocurence of a particular word from a sentence.it gives me a wrong output it starts checking for the same alphabets in the sentence rather than the word.
import java.lang.String;
public class StringCount2
{
public static void main(String args[])
{
String searchFor = "is";
String base = "This is object oriented programming language ";
int len = searchFor.length();
//System.out.println( "length of object =" +len);
int result = 0;
if (len > 0)
{
int start = base.indexOf(searchFor);
// System.out.println("index of object =" + start);
while (start != -1)
{
result++;
start = base.indexOf(searchFor, start+len);
}
}
System.out.println(result);
}
}
can any one help me with this.