-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrequencyofstring.java
More file actions
42 lines (32 loc) · 1.03 KB
/
Frequencyofstring.java
File metadata and controls
42 lines (32 loc) · 1.03 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
39
40
41
import java.util.Scanner;
public class Frequencyofstring {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String s = sc.nextLine();
frequency(s);
sc.close();
// for (int i = 0; i < s.length(); i++) {
// char ch = s.charAt(i);
// int count = 0;
// for (int j = 0; j < s.length(); j++) {
// if (s.charAt(j) == ch) {
// count++;
// }
// }
// System.out.println(count+" "+s.charAt(i));
// }
}
public static int[] frequency(String s){
int[] count = new int[128];
for (int i = 0; i < s.length(); i++) {
char c=s.charAt(i);
count[c]++;
}
for (int i = 0; i <count.length; i++) {
if(count[i]!=0){
System.out.println((char)i+" "+count[i]);
}
}
return count;
}
}