Computer Science, asked by Niharika1415, 1 year ago

write a Java Program to input a sentence and print the frequency of a particular word inputed by the user...


plz answer it correctly today itself as I have exam today... correct answer will be marked as brainliest..
thankyou..

Answers

Answered by DamanpreetDhillon
16
.I think this is ur ans dear.....sorry if m writen the ans wrong...
.
.

Write a Program in Java To Enter a String and find the frequency of A Specific word present in that String
Write a Java Program To Find the Frequency of a word present in that String
In this program you have been asked to enter a string, probably a sentence or group of words, and you have to then provide a word as the user input to look for the frequency of that specific word in that string.

SAMPLE:

Tata Steel is one of the Steel manufacturing company of the country;

Frequency of the word to be searched : the
OUTPUT : 2

Programming Code:

import java.io.*;
public class string2
{
public static void main(String args[])throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);

String word="";
int sp=0;
int c=0;

System.out.println("Enter your String");
String sentence = in.readLine();
sentence=sentence+" ";
int len=sentence.length();
System.out.println("Enter the word whose frequency is to be found");
String search = in.readLine();

for(int i=0;i {
char ch=sentence.charAt(i);
if (ch==' ')
{
word = sentence.substring(sp,i);
sp=i+1;

if(word.equalsIgnoreCase(search)==true)
{
c++;
}
}
}

System.out.println("The number of substring "+search+" in the string is "+c);
}
}
Program Execution:

Enter your String
Einstein was called the best scientist the Germans and the world has witnessed.
Enter the word whose frequency is to be found
The number of substring the in the string is 3.

Categories All Programs, String
Tags frequency, string, word
Post navigation
Input 3 Numbers And Check Whether They Are Pythagorean Triplet Or Not
Java Program to Input a String and Check Wether its Palindrome...

preetghotra: hi
Similar questions