简体   繁体   English

编写程序以打印每个字母以及用户输入中出现了多少个字母

[英]Write a program to print each and every alphabet with how many occured in a user input

I have to write a program to accept a String as input, and as output I'll have to print each and every alphabetical letter, and how many times each occurred in the user input. 我必须编写一个程序来接受String作为输入,并且作为输出,我必须打印每个字母字母,以及每个字母在用户输入中出现了多少次。 There are some constraints: 有一些限制:

  • I cannot use built-in functions and collection 我无法使用内置函数和集合
  • The printed result should be sorted by occurrence-value. 打印结果应按出现值排序。

For example, with this input: 例如,使用以下输入:

abbbccccdddddzz abbbccccdddddzz

I would expect this output: 我期望这个输出:

a-1,z-2,b-3,c-4,d-5 A-1,Z-2,B-3,C-4,d-5

This is what I have so far: 这是我到目前为止的内容:

public static void isCountChar(String s) {
    char c1[] = s.toCharArray();
    int c3[] = new int[26];
    for (int i = 0; i < c1.length; i++) {
        char c = s.charAt(i);
        c3[c - 'a']++;
    }

    for (int j = 0; j < c3.length; j++) {
        if (c3[j] != 0) {
            char c = (char) (j + 'a');
            System.out.println("character is:" + c + " " + "count is:  " + c3[j]);
        }
    }
}

But I don't know how to sort. 但是我不知道如何排序。

some sort: easy if not easiest to understand an coding 某种形式:容易,即使不是最不容易理解的编码

  • You loop from the first element to the end -1 : element K 您从第一个元素循环到结尾-1:元素K

  • compare element K and element K+1: if element K>element K+1, invert them 比较元素K和元素K + 1:如果元素K>元素K + 1,将它们求逆

  • continue loop 继续循环

  • if you made one change redo that ! 如果您进行了一次更改,请重做!

First of all a tip for your next question: The things you've stated in your comments would fit better as an edit to your question. 首先,提出下一个问题的技巧:您在评论中陈述的内容更适合作为对问题的编辑。 Try to clearly state what the current result is, and what the expected result should be. 尝试清楚说明当前结果是什么,以及预期结果应该是什么。

That being said, it was an interesting problem, because of the two constraints. 话虽如此,由于两个限制,这是一个有趣的问题。

  • First of all you weren't allowed to use libraries or collections. 首先,您不允许使用库或集合。 If this wasn't a constraint I would have suggested a HashMap with character as keys, and int as values, and then the sorting would be easy. 如果这不是一个约束,我会建议使用字符作为键,将int作为值的HashMap ,然后排序就很容易了。
  • Second constraint was to order by value. 第二个约束是按价值排序。 Most people here suggested a sorting like BubbleSort which I agree with, but it wouldn't work with your current code because it would sort by alphabetic character instead of output value . 这里的大多数人都建议使用类似于BubbleSort的排序方式,但我同意,但是它不适用于您当前的代码,因为它将按alphabetic character而不是output value排序。

With these two constraints it is probably best to fake key-value pairing yourself by making both an keys -array and values -array, and sort them both at the same time (with something like a BubbleSort -algorithm). 有这两个约束,最好使自己的key-value对伪造出来,方法是同时创建keys -array和values -array,并同时对它们进行排序(使用BubbleSort -algorithm之类的方法)。 Here is the code: 这是代码:

private static final int ALPHABET_SIZE = 26;

public static void isCountChar(String s)
{
    // Convert input String to char-array (and uppercase to lowercase)
    char[] array = s.toLowerCase().toCharArray();

    // Fill the keys-array with the alphabet
    char[] keys = new char[ALPHABET_SIZE];
    for (int i = 0; i < ALPHABET_SIZE; i++)
    {
        keys[i] = (char)('a' + i);
    }

    // Count how much each char occurs in the input String
    int[] values = new int[ALPHABET_SIZE];
    for (char c : array)
    {
        values[c - 'a']++;
    }

    // Sort both the keys and values so the indexes stay the same
    bubbleSort(keys, values);

    // Print the output:
    for (int j = 0; j < ALPHABET_SIZE; j++)
    {
        if (values[j] != 0)
        {
            System.out.println("character is: " + keys[j] + "; count is: " + values[j]);
        }
    }
}

private static void bubbleSort(char[] keys, int[] values)
{
    // BUBBLESORT (copied from http://www.java-examples.com/java-bubble-sort-example and modified)
    int n = values.length;

    for(int i = 0; i < n; i++){
        for(int j = 1; j < (n - i); j++){        
            if(values[j-1] > values[j]){
                // Swap the elements:
                int tempValue = values[j - 1];
                values[j - 1] = values[j];
                values[j] = tempValue;

                char tempKey = keys[j - 1];
                keys[j - 1] = keys[j];
                keys[j] = tempKey;
            }         
        }
    }
}

Example usage: 用法示例:

public static void main (String[] args) throws java.lang.Exception
{
    isCountChar("TestString");
}

Output: 输出:

character is: e; count is: 1
character is: g; count is: 1
character is: i; count is: 1
character is: n; count is: 1
character is: r; count is: 1
character is: s; count is: 2
character is: t; count is: 3

Here is a working ideone to see the input and output. 这是查看输入和输出的工作思路

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从用户输入和降序开始打印字母金字塔? - How to print alphabet pyramid starting with user input and descending? 获取用户输入,放入数组并打印出每个字母的使用次数 - Take user input, put into an array and print out how many times each letter is used 使用一维数组编写一个程序,该数组具有来自随机生成器的值,以打印出每个组合被一对骰子滚动多少次 - Write a program using 1-D arrays with values from a random generator to print out how many times each combination was rolled by a pair of dice 如何编写一个以用户标记为输入的程序? - How write a program that takes the marks of user as input? 编写一个程序,确定一个名字中每个字母有多少个 - Write a program that determines how many of each letter a name has 如何输入并让程序在新行上打印出每个字母,同时遇到每个空格以打印出“ <space> “? - How do I take an input and make the program print out each letter on a new line while making every space encounter to print out “<space>”? 岩石,纸张,蜥蜴,麻雀。 用户必须输入100个游戏,并且程序应给出选择了每个游戏的结果 - Rock, paper, lizard, spock. User has to input 100 games and it program should give the outcome of how many of each were choosen 如何打印ASCII值的字母 - How to print alphabet of ascii value 程序以显示值是否为字母,如果是数字,则打印“ Alphabet”,然后打印“ Digit”,对于其他字符,则打印“ Special Character”? - program to show if the value is alphabet then print “Alphabet” if it’s a number then print “Digit” and for other characters print “Special Character”? 如何编写自动向 java 程序输入提示用户输入的批处理文件 - How to write a batch file that automatically gives input to a java program that prompts for user input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM