
java prgm to find total occurence of a given string pattern in a sentence

Hi Friend,
Try the following code:
import java.util.*;
class TotalOccurrences
{
public static void main(String[] args)
{
int count=0;
Scanner input=new Scanner(System.in);
System.out.print("Enter Sentence: ");
String str=input.nextLine();
System.out.print("Enter string to search: ");
String st=input.nextLine();
StringTokenizer stk=new StringTokenizer(str);
while(stk.hasMoreTokens()){
String s=stk.nextToken();
if(s.equals(st)){
count++;
}
}
System.out.println(st+" occurs "+count+" times in a sentence");
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.