-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOccuranceOfEachCharacter.java
70 lines (56 loc) · 1.52 KB
/
OccuranceOfEachCharacter.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
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
63
64
65
66
67
68
69
70
package coding;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.testng.annotations.Test;
public class OccuranceOfEachCharacter extends BaseTestNg{
String str = "welcome to automation";
int count =0;
@Test(priority=1)
public void main() {
char[] char_array =str.toCharArray();
Map<Character,Integer> charCounter=new HashMap<Character,Integer>();
for (char ch : char_array) {
if(charCounter.containsKey(ch)){
charCounter.put(ch, charCounter.get(ch)+1);
}
else{
charCounter.put(ch, 1);
}
}
System.out.println(charCounter);
}
@Test(priority=2)
public void occurance() {
String string="";
int temp =0;
Map<Integer,String> map =new LinkedHashMap<Integer,String>();
for (int i = 0; i < str.length(); i++) {
map.put(temp, ""+str.charAt(i));
temp++;
}
for (int eachChar : map.keySet()) {
count=0;
string =map.get(eachChar);
for (Integer eachKey : map.keySet()) {
if (string.equals(map.get(eachKey))) {
count++;
}
}
System.out.println(map.get(eachChar)+"-->"+count);
}
}
@Test(priority=3)
public void occuranceOfString() {
for (int j = 0; j < str.length(); j++) {
count = 0;
char c = str.charAt(j);
for (int i = 0; i < str.length(); i++) {
if (c == str.charAt(i)) {
count++;
}
}
System.out.println(c + " occurs " + count + " times in " + str); // you can store this in Map and print for better solution
}
}
}