Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class siraCountLetters {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.print("Please enter some text:");
- String userInput = input.nextLine().toLowerCase();
- int[] array = new int[('z' - 'a') + 1];
- char[] charactersSorted = new char[('z' - 'a') + 1];
- for (int i = 0; i < userInput.length(); i++) {
- char ch = userInput.charAt(i);
- if (ch >= 'a' && ch <= 'z') {
- charactersSorted[ch - 'a'] = ch;
- array[ch - 'a']++;
- }
- }
- for (int i = 1; i < array.length; i++) {
- for (int k = 0; k < array.length - i; k++) {
- int tmpCharCount = array[k];
- char tmpChar = charactersSorted[k];
- if (array[k + 1] > 0) {
- if (array[k + 1] > array[k]) {
- array[k] = array[k + 1];
- array[k + 1] = tmpCharCount;
- charactersSorted[k] = charactersSorted[k + 1];
- charactersSorted[k + 1] = tmpChar;
- }
- }
- }
- }
- System.out.println("Most common letters:");
- for (int i = 0; i < array.length; i++) {
- if (array[i] > 0) {
- String hashes = "";
- for (int j = 0; j < array[i]; j++) {
- hashes = hashes + "##";
- }
- System.out.println(charactersSorted[i] + ": " + hashes);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement