Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args)
- {
- // TODO Auto-generated method stub
- String str = "I love programming ";
- System.out.println (letterFrequencies(input));
- }
- public static int timesCharOccurs (String str, char character)
- {
- int timesOccurs = 0;
- String str2 = str.toLowerCase();
- char [] charArray = str2.toCharArray(); // Turns the String into Char
- for (int i=0; i<str2.length(); i++) // Loops for the number of Chars as transformed
- {
- if (charArray[i] == character)
- {
- timesOccurs ++;
- }
- }
- return timesOccurs;
- }
- public static int[] letterFrequencies (String input)
- {
- int [] occuranceValues = new int[26];
- char [] alphabetArray = {
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
- 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
- for (int i=0; i < alphabetArray.length; i++)
- {
- char letter = alphabetArray[i];
- occuranceValues[i] = timesCharOccurs(input, letter);
- }
- return occuranceValues;
- }
- System.out.println (letterFrequencies(input));
- System.out.println (letterFrequencies(str));
Add Comment
Please, Sign In to add comment