5 Dec 2017

write a program in Java to find the frequency of character or occurrence of each character in given string.



import java.util.*;
  class FrequencyOfCharacters
  {
          public static void main(String[] args)
          {
                   System.out.println("Enter the string");
                   String s=new Scanner(System.in).nextLine();
                   frequency(s);
          }
          public static  void frequency(String str)
          {
                   Character c;
                   HashMap hm=new HashMap();
                   for(int i=0;i<str.length();i++)
                   {
                             c=str.charAt(i);
                             if(hm.containsKey(c))
                                      hm.put(c,hm.get(c)+1);
                             else
                                      hm.put(c,1);
                   }
                   Set keySet = hm.keySet();
                   Iterator keySetIterator = keySet.iterator();
                   while (keySetIterator.hasNext()) {
                   Character key = keySetIterator.next();
                   System.out.println(key + " " + hm.get(key));
          }
  }
 }



OutPut