WRITE A PROGRAM IN JAVA TO ENTER A SENTENCE AND COUNT THE NUMBER OF TIMES A PARTICULAR WORD OCCURS IN THE SENTENCE.
THE FOLLOWING PROGRAM SEGMENT SHOWS HOW TO COUNT THE NUMBER OF TIMES A PARTICULAR WORD OCCURS IN A SENTENCE.
import java.util.*;
class frqword
{
public static void main (String args[])
{
Scanner ob = new Scanner (System.in);
System.out.println("Enter a sentence");
String s = ob.nextLine();
String w, word = "";
char ch;
int c = 0;
s = s.trim();
System.out.println("Enter a word to search");
w = ob.nextLine();
s = s+"";
for(int i = 0; i<s.length(); i++)
{
ch = s.charAt(i);
if(ch != ' ')
{
word = word+ch;
}
else
{
if(w.equals(word))
{
c++;
}
word = "";
}
}
System.out.println("Searched word occurs " + c + "times in the sentence");
}
}
Hope you guys found it helpful.
Please share it with your friends and leave a comment in the comment section below.
Follow https://askrisfor.blogspot.com/ for more such programs.
Thank you !!!
Visit again.
Comments
Post a Comment