1042. Character Statistics (20) String Operations 9

  1. Character Statistics(20)

Please write a program to find the English letter that occurs most frequently in a given text.

Input format:

Input gives a string of length up to 1000 in one line. The string consists of any visible characters and spaces in the ASCII code table, contains at least one English letter, and ends with a carriage return (the carriage return is not included).

Output format:

Output the English letter with the highest frequency and the number of occurrences in one line, separated by spaces. If there is a tie, output the letter with the smallest alphabetical order. Statistics are not case-sensitive, and output lowercase letters.

Input sample:
This is a simple TEST. There ARE numbers and other symbols 1&2&3………..Output
sample:
e 7

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        // use string to hold the input
        String string = in.nextLine();

        // create a array which length is 26
        int[] array = new int[26];
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);

            if (c >= 'a' && c <= 'z') {
                array[c - 'a']++;
            } else if (c >= 'A' && c <= 'Z') {
                array[c - 'A']++;
            } else {
                //except character
                //like number, coin sign and other letters.
            }
        }

        in.close();

        int maxIndex = 0;
        for (int i = 1; i < array.length; i++) {
            if (array[i] > array[maxIndex]) {
                maxIndex = i;
            }
        }
        System.out.println((char)(maxIndex + 'a') + " " + array[maxIndex]);
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324695798&siteId=291194637