编写一个程序来打印每个字母表,其中包含用户输入中出现的数量

时间:2015-12-03 13:12:10

标签: java

我必须编写一个程序来接受一个String作为输入,并且作为输出,我必须打印每个字母,以及每个字母在用户输入中出现的次数。有一些限制:

  • 我不能使用内置函数和集合
  • 打印结果应按出现次数排序。

例如,使用此输入:

  

abbbccccdddddzz

我希望这个输出:

  

A-1,Z-2,B-3,C-4,d-5

这是我到目前为止所做的:

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]);
        }
    }
}

但我不知道如何分类。

2 个答案:

答案 0 :(得分:0)

某种:如果不是最容易理解编码那么容易

  • 从第一个元素循环到结束-1:元素K

  • 比较元素K和元素K + 1:如果元素K&gt;元素K + 1,则反转它们

  • 继续循环

  • 如果你做了一次改造,那就是重做!

答案 1 :(得分:0)

首先提供下一个问题的提示:您在评论中说明的内容更适合作为对问题的修改。尝试清楚地说明当前结果是什么,以及预期结果应该是什么。

话虽如此,这是一个有趣的问题,因为有两个限制因素。

  • 首先,您不得使用库或集合。如果这不是一个约束,我会建议HashMap将字符作为键,将int作为值,然后排序很容易。
  • 第二个约束是按价值排序。这里的大多数人都提出了类似BubbleSort的排序,我同意这一点,但它不适用于您当前的代码,因为它会按alphabetic character而不是output value排序。

有了这两个限制,最好假设key-value通过制作keys - 数组和values - 数组来对自己进行配对,并同时对它们进行排序(使用类似于BubbleSort - 算法的东西。这是代码:

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;
            }         
        }
    }
}

使用示例:

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

输出:

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.