forked from TestLeafPages/JavaPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOccuranceOfCharacter.java
More file actions
65 lines (53 loc) · 1.38 KB
/
OccuranceOfCharacter.java
File metadata and controls
65 lines (53 loc) · 1.38 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
63
64
65
package coding;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.testng.annotations.Test;
public class OccuranceOfCharacter 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 occuranceOfString() {
char c = str.charAt(0);
for (int i = 0; i < str.length(); i++) {
if (c == str.charAt(i)) {
count++;
}
}
System.out.println(c + " occurs " + count + " times in " + str);
}
@Test(priority=3)
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);
}
}
}