Computer Science, asked by ujjwalkumar637151, 5 hours ago

Using methods charAt() & length() of String class, write a program to print the frequency of each character in a string. “Hello friend”

Output should be -:
1
d: 1
e: 2
f: 1
(continued for all character in the string)​

Answers

Answered by yashsaxena8055
0

Answer:

Hashmap<Character, Integer> map = Map<>();

String string = "Hello Friend";

for (int i =0 ; i < string.length(); i++)

{

if(string.charAt(i)!=' ')

{

if(map.contains(charAt(i)))

{

map.put(charAt(i) , map.get(charAt(i)) +1);

}

else

{

map.put(charAt(i),1);

}

}

}

for (Character key: map.keySet()) {

System.out.println(key+" : "+map.get(key));

}

Similar questions