Counting characters in a Character array


Sounds like computer science is not your cup of tea. I Strongly recommend you refactor this code and try to figure out why it does what it does.

public void print(String ht) { // print array

    String st = ht.toLowerCase().replaceAll("s", "");
    sent = st.toCharArray();

    int[] alphabet = new int[26];
    //set all values to 0
    for(int i = 0 ; i < alphabet.length ; i++){
        alphabet[i] = 0;
    }
    //Loop through all characters and increment corresponding value
    for(int i = 0 ; i < sent.length ; i++){
        alphabet[sent[i] - 97]++;
    }
    //Print character + asterisk for each time the character is used
    for(int i = 0 ; i < alphabet.length ; i++){
        System.out.print((char)(i + 97) + ": ");
        for(int nChars = 0 ; nChars < alphabet[i] ; nChars++){
            System.out.print("*");
        }
        System.out.println();
    }

}
链接地址: http://www.djcxy.com/p/20312.html

上一篇: Python字计数器

下一篇: 计数字符数组中的字符