Algorithm for Number of Times a Word has Been Mentioned :
step 1: read String w
step 2: read String w1
step 3: set len=w1.length()
step 4: set result=0
step 5: If len> 0 then executes following steps else goto step-10
step 6: Set start =w.indexof(w1, start+len)
step 7: repeat through step-9 while(start 1= -1)
step 8: result= result+ 1
step 9: start = w. indexof(w1, start+ len)
step 10: print result
step 11: Exit
Here is the Java Example for – Number of Times a Word has Been
import java.util.*; import java.io.*; public class WordCount { public static void main(String args[]) { System.out.print("Please Enter the Sentence : "); Scanner Word=new Scanner(System.in); String Count=Word.nextLine(); System.out.print("Please Enter the Word : "); String wl=Word.nextLine(); int len=wl.length(); int CountWord =0; if(len> 0) { int start = Count.indexOf(wl); while(start!=-1) { CountWord++; start=Count.indexOf(wl,start+len); } } System.out.println(CountWord); } }