-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrequencyOOPS.java
More file actions
62 lines (47 loc) · 1.44 KB
/
FrequencyOOPS.java
File metadata and controls
62 lines (47 loc) · 1.44 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.*;
import java.util.Scanner;
import java.util.ArrayList;
public class FrequencyOOPS {
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter the first text :- ");
String text1= sc.nextLine();
System.out.println("For " + text1);
frequency(text1);
System.out.println("Enter the second text :- ");
String text2= sc.nextLine();
System.out.println("For " + text2);
frequency(text2);
}
private static void frequency(String s){
if (s.length() == 0){
System.out.println("String is empty");
}
ArrayList<CharOccur> occurences= new ArrayList<CharOccur>();
for(int i=0;i<s.length();i++){
char c= s.charAt(i);
int flag=0;
for(CharOccur o : occurences){
if(o.character == c ){
o.occurence += 1;
flag=1;
}
}
if(flag == 0){
CharOccur grp= new CharOccur(c,1);
occurences.add(grp);
}
}
for(CharOccur o : occurences){
System.out.println(o.character + " " + o.occurence);
}
}
}
class CharOccur{
char character;
int occurence=0;
CharOccur(char character, int occurence){
this.character= character;
this.occurence= occurence;
}
}