write a java program to find the frequency of the searched word in a string.
Sample input: Tata steel is a famous manufacturing company of steel.
Sample output: Steel is printed 2 times
Plzz send it fast
I will mark u brainliest
Answers
Explanation:
SOURCE CODE :
import java.util.Scanner;
public class Count_Words
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter any sentence below : ");
String text = sc.nextLine();
String[] keys = text.split(" ");
String[] uniqueKeys;
int count = 0;
uniqueKeys = getUniqueKeys(keys);
for(String key: uniqueKeys)
{
if(null == key)
{
break;
}
for(String s : keys)
{
if(key.equals(s))
{
count++;
}
}
System.out.println("Count of [ "+key+" ] is : "+count);
count=0;
}
}
private static String[] getUniqueKeys(String[] keys)
{
String[] uniqueKeys = new String[keys.length];
uniqueKeys[0] = keys[0];
int uniqueKeyIndex = 1;
boolean keyAlreadyExists = false;
for(int i=1; i<keys.length ; i++)
{
for(int j=0; j<=uniqueKeyIndex; j++)
{
if(keys[i].equals(uniqueKeys[j]))
{
keyAlreadyExists = true;
}
}
if(!keyAlreadyExists)
{
uniqueKeys[uniqueKeyIndex] = keys[i];
uniqueKeyIndex++;
}
keyAlreadyExists = false;
}
return uniqueKeys;
}
}
OUTPUT :
Enter any sentence below :
This is programming website whose name is CodezClub
Count of [ This ] is : 1
Count of [ is ] is : 2
Count of [ programming ] is : 1
Count of [ website ] is : 1
Count of [ whose ] is : 1
Count of [ name ] is : 1