Guest User

Untitled

a guest
Nov 12th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. public static void main(String[] args)
  2. {
  3. // TODO Auto-generated method stub
  4. String str = "I love programming ";
  5.  
  6. System.out.println (letterFrequencies(input));
  7. }
  8.  
  9. public static int timesCharOccurs (String str, char character)
  10. {
  11. int timesOccurs = 0;
  12.  
  13. String str2 = str.toLowerCase();
  14. char [] charArray = str2.toCharArray(); // Turns the String into Char
  15. for (int i=0; i<str2.length(); i++) // Loops for the number of Chars as transformed
  16. {
  17. if (charArray[i] == character)
  18. {
  19. timesOccurs ++;
  20. }
  21. }
  22. return timesOccurs;
  23. }
  24.  
  25. public static int[] letterFrequencies (String input)
  26. {
  27. int [] occuranceValues = new int[26];
  28. char [] alphabetArray = {
  29. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  30. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  31.  
  32. for (int i=0; i < alphabetArray.length; i++)
  33. {
  34. char letter = alphabetArray[i];
  35. occuranceValues[i] = timesCharOccurs(input, letter);
  36. }
  37. return occuranceValues;
  38. }
  39.  
  40. System.out.println (letterFrequencies(input));
  41.  
  42. System.out.println (letterFrequencies(str));
Add Comment
Please, Sign In to add comment