Advertisement
PetyoKamenov

Untitled

Jul 22nd, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class siraCountLetters {
  4. public static void main(String[] args) {
  5.  
  6. Scanner input = new Scanner(System.in);
  7. System.out.print("Please enter some text:");
  8. String userInput = input.nextLine().toLowerCase();
  9. int[] array = new int[('z' - 'a') + 1];
  10. char[] charactersSorted = new char[('z' - 'a') + 1];
  11. for (int i = 0; i < userInput.length(); i++) {
  12. char ch = userInput.charAt(i);
  13. if (ch >= 'a' && ch <= 'z') {
  14. charactersSorted[ch - 'a'] = ch;
  15. array[ch - 'a']++;
  16. }
  17. }
  18. for (int i = 1; i < array.length; i++) {
  19. for (int k = 0; k < array.length - i; k++) {
  20. int tmpCharCount = array[k];
  21. char tmpChar = charactersSorted[k];
  22. if (array[k + 1] > 0) {
  23. if (array[k + 1] > array[k]) {
  24. array[k] = array[k + 1];
  25. array[k + 1] = tmpCharCount;
  26. charactersSorted[k] = charactersSorted[k + 1];
  27. charactersSorted[k + 1] = tmpChar;
  28. }
  29. }
  30. }
  31. }
  32. System.out.println("Most common letters:");
  33. for (int i = 0; i < array.length; i++) {
  34. if (array[i] > 0) {
  35. String hashes = "";
  36. for (int j = 0; j < array[i]; j++) {
  37. hashes = hashes + "##";
  38. }
  39. System.out.println(charactersSorted[i] + ": " + hashes);
  40. }
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement