//Program to calculate the frequency of each character in given String
import java.io.*; //for IOExecption, BufferedReader, InputStreamReader
class CharacterFrequency
{
  public static void main(String args[]) throws IOException
  {
     System.out.println("Enter the string: ");
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     String s=br.readLine(); //gets a string from user
     String temp=s;
     System.out.println("\nFollowing are the characters in string");
    
     while(temp.length()!=0)
     {
        char ch=temp.charAt(0);
        int count=0;
        for(int i=0;i<temp.length();i++)
          if(temp.charAt(i)==ch)
            count++;
        System.out.println(ch+": "+count);
     temp=temp.replace(ch+"","");  //replaces the current character with empty string
     }
  }
}
                                     By Er Gurpreet Singh,
Senior Software Engineer,
Sopra Steria India
Saturday, 13 June 2015
Program to calculate the frequency of each character in given String
Labels:
Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment