Showing posts with label number of times word occurs. Show all posts
Showing posts with label number of times word occurs. Show all posts

Tuesday, March 23, 2010

Count Number Of Times Word Occurs

import java.io.*;

public class position
{ public static void main(String args[]) throws IOException
{ InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);

System.out.println("Enter a sentence");
String a=br.readLine();
System.out.println("Enter word to be searched for");
String word = br.readLine();

int count=0;
int pos=-1;
pos = a.indexOf(word);

while(pos>=0)
{ count++;
pos = pos + word.length();
pos = a.indexOf(word,pos);
}
System.out.println("Number of times word occurs " +count);
System.out.println(pos);
}
}