prograames

prograames

write a program to find the frequency of each character in a string?

View Answers

June 13, 2011 at 5:26 PM

Hi Abhishek

Go through the following link, it will give you a clear idea :

http://www.roseindia.net/java/java-tips/data/collections/maps/ex-wordfreq.shtml


June 15, 2011 at 10:37 AM

import java.util.*;

class  CountCharacters {
  public static void main(String[] args) throws Exception{
  Scanner input=new Scanner(System.in);
  System.out.print("Please enter string ");
  System.out.println();
  String str=input.nextLine();
  String st=str.replaceAll(" ", "");

  char[]third =st.toCharArray();
  for(int counter =0;counter<third.length;counter++){
  char ch= third[counter];
  int count=0;
  for ( int i=0; i<third.length; i++){
  if (ch==third[i])
  count++;
}
boolean flag=false;
for(int j=counter-1;j>=0;j--){
if(ch==third[j])
flag=true;
}
if(!flag){
System.out.println("Character :"+ch+" occurs "+count+" times ");
}
}
}
}









Related Tutorials/Questions & Answers:
prograames

Ads