-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLetterOccurence.java
33 lines (27 loc) · 1.03 KB
/
LetterOccurence.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
//This program calculates the number of letters in a string
import java.util.*;
public class LetterOccurence
{
public static void main (String[] args)
{
Map<Character, Integer> m = new HashMap<>();
String str = "Hello My Name is Omar and I am super social";
for(char x:str.toCharArray())//take the string and place it inside a character Array
{
//Now we will check how many times a character exists within the array
//If it already exists have it update the value
if(m.containsKey(x))
{
int old=(int)m.get(x);
m.put(x,old+1);
}
//If it doesn't exist create a new key and give it a value of 1
else
{
m.put(x,1);
}
}
//Print the occurence of the letters to the console
System.out.println(m);
}
}