-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrequencyOfCharacters.java
More file actions
34 lines (28 loc) · 919 Bytes
/
Copy pathFrequencyOfCharacters.java
File metadata and controls
34 lines (28 loc) · 919 Bytes
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
import java.util.Scanner;
public class FrequencyOfCharacters {
static void frequency(char c[]){
System.out.println("Calculating frequency of characters: ");
boolean visited[]= new boolean[c.length];
for(int i=0; i<c.length; i++){
if (visited[i]== false){
int count= 1;
for(int j=i+1; j<c.length; j++){
if(c[i]==c[j]){
visited[j]= true;
count++;
}
}
System.out.println(c[i] + " - " + count );
}
}
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
//STRING INPUT
System.out.print("Enter a string: ");
String s= sc.nextLine();
//CONVERT THE STRING TO CHAR ARRAY
char c[]= s.toCharArray();
frequency(c);
}
}