Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Letters
- {
- /**
- Counts the frequencies of letters A-Za-z in a string
- @param str a string
- @return an array of 26 percentages. The i-th count is the percentage of occurrences
- of 'A' + i or 'a' + i among all letters (a number between 0.0 and 1.0).
- */
- public double[] letterFrequencies(String str)
- {
- str.toLowerCase();
- double[] result = new double[26];
- char[] arrStr = str.toCharArray();
- int numberOfLetters = 0;
- for(char a : arrStr)
- {
- if(Character.isLetter(a))
- {
- result[Character.getNumericValue(a) - 10] += 1;
- numberOfLetters++;
- }
- }
- for(int i = 0; i < result.length; i++)
- {
- result[i] /= numberOfLetters;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment