advictoriam

Untitled

Jan 23rd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. public class Letters
  2. {
  3.    /**
  4.       Counts the frequencies of letters A-Za-z in a string
  5.       @param str a string
  6.       @return an array of 26 percentages. The i-th count is the percentage of occurrences
  7.       of 'A' + i or 'a' + i among all letters (a number between 0.0 and 1.0).
  8.    */
  9.    public double[] letterFrequencies(String str)
  10.    {
  11.       str.toLowerCase();
  12.       double[] result = new double[26];
  13.       char[] arrStr = str.toCharArray();
  14.       int numberOfLetters = 0;
  15.      
  16.       for(char a : arrStr)
  17.       {
  18.          if(Character.isLetter(a))
  19.          {
  20.             result[Character.getNumericValue(a) - 10] += 1;
  21.             numberOfLetters++;
  22.          }
  23.       }
  24.      
  25.       for(int i = 0; i < result.length; i++)
  26.       {
  27.          result[i] /= numberOfLetters;
  28.       }
  29.      
  30.       return result;
  31.    }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment