forked from harshitbansal373/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEachCharCountInString.java
More file actions
44 lines (31 loc) · 1.05 KB
/
EachCharCountInString.java
File metadata and controls
44 lines (31 loc) · 1.05 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
import java.util.HashMap;
class EachCharCountInString
{
static void characterCount(String inputString)
{
//Creating a HashMap containing char as a key and occurrences as a value
HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();
//Converting given string to char array
char[] strArray = inputString.toCharArray();
//checking each char of strArray
for (char c : strArray)
{
if(charCountMap.containsKey(c))
{
//If char is present in charCountMap, incrementing it's count by 1
charCountMap.put(c, charCountMap.get(c)+1);
}
else
{
//If char is not present in charCountMap,
//putting this char to charCountMap with 1 as it's value
charCountMap.put(c, 1);
}
}
System.out.println(charCountMap);
}
public static void main(String[] args)
{
characterCount("All Is Well");
}
}