-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrequency.java
33 lines (33 loc) · 1.16 KB
/
Frequency.java
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
//Assignment 16
import java.util.*;
class Frequency{
private String word;//stores the user's entered word
private String nword;//stores the modified word
public void input(){//takes input from the user
System.out.print("Enter a String: ");
word=new Scanner(System.in).next().toUpperCase();
nword=word;
}//end of input()
public int count(char ch){//calculate the frequency of a character ch
int c=0;//counter
String n1word="";
for(int i=0;i<nword.length();i++){
if(ch==nword.charAt(i))
c++;
else
n1word= n1word + Character.toString(nword.charAt(i));
}//end of for loop
nword=n1word;
return c;
}//end of count()
public void traverse(){//traverse through the modified word
System.out.println("Word: "+word);
while(nword.length()!=0)
System.out.println("Frequency of "+(nword.charAt(0))+" : "+count(nword.charAt(0)));
}//end of traverse()
public static void main(String args[]){
Frequency ob=new Frequency();
ob.input();
ob.traverse();
}//end of main()
}//end of class