Ans..
Here we can count the character occurrence in String in very simple way. first we have to declare String that for every character and count of that character we have to define Hashmap type of Character and for count Integer as value.then Iterate through String value and assign the value in char c by using for loop.
after that check the key if key in present than get the value of the key and store it into int count variable and put the value key as Ch and value as count+1;
if key as ch is not available in Map than add the key and value as 1.
See below program.
for video go click Here
import java.util.HashMap;
public class StringCount {
public static void main(String[] args) {
String str = "Programming22222";
HashMap<Character, Integer> map = new HashMap<>();
for (char ch : str.toCharArray()) {
if (map.containsKey(ch)) {
int count = map.get(ch);
map.put(ch, count + 1);
} else {
map.put(ch, 1);
}
}
System.out.println(map);
}
}
Good..very good
ReplyDelete