-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfrequency_of_every_word.java
More file actions
38 lines (38 loc) · 1.05 KB
/
frequency_of_every_word.java
File metadata and controls
38 lines (38 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//44.C Program to Find the Frequency of Every Word in a given String
import java.util.*;
class frequency_of_every_word
{
public static void count_freq(String str)
{
String words[]=str.split(" ");
int l=words.length;
int arr[]=new int[l];
for(int i=0;i<l;i++)
{
arr[i]=1;
}
int count=1;
for(int i=0;i<l;i++)
{
for(int j=i+1;j<l;j++)
{
if(words[i].compareTo(words[j])==0)
{
count++;
arr[j]=-1;
words[j]=words[j].replaceAll(words[j], "");
}
}
if(arr[i]!=-1)
{System.out.println("The Frequency of word : "+words[i]+" = "+count);
count=1;}
}
}
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the string : ");
String str=new String(sc.nextLine());
count_freq(str);
}
}