participate


New To Java - frequency string
This question is not answered.

<<   Back to Forum  |   Give us Feedback
10 Duke Stars available
This topic has 3 replies on 1 page.
smita_smita
Posts:502
Registered: 12/16/04
frequency string   
Nov 7, 2009 7:44 AM
 
 
how the frequency of character can be counted optimally in java? thanks
 
DarrylBurke
Posts:15,598
Registered: 7/2/07
Re: frequency string   
Nov 7, 2009 9:18 AM (reply 1 of 3)  (In reply to original post )
 
 
You tell us. After all, it's your homework.

db
 
prometheuzz
Posts:15,134
Registered: 7/28/05
Re: frequency string   
Nov 7, 2009 12:02 PM (reply 2 of 3)  (In reply to original post )
 
 
smita_smita wrote:
how the frequency of character can be counted optimally in java? thanks

By iterating over the characters of the string and counting how much each character occurs.
 
_Anti
Posts:10
Registered: 11/7/09
Re: frequency string   
Nov 8, 2009 8:05 AM (reply 3 of 3)  (In reply to #2 )
 
 
        String text="ghkhwehrgjklwheorhnfgpwjelrgjwelrg";
        text=text.toLowerCase();
        int charNum=26; // the number of unique characters
 
        int[] freq=new int[charNum];
 
        for(int i=0;i<text.length();i++) {
            freq[text.charAt(i)-'a']++;
        }
 
        for(int i=0;i<charNum;i++) {
            System.out.println("Frequency of " + (char) (97 + i) +": " + freq[i]);
        }
 
This topic has 3 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 29
  • Guests : 131

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums