我如何返回字母c的出现次数

时间:2016-04-29 06:44:52

标签: java

这是我的代码:

public int frequency (char c)
{
    int numOccurrences = 0;
    String ltr = "letters";
    ltr = ltr.toLowerCase();
    for (int i = 0; i < ltr.length(); i++) 
    {
        if (l.charAt(0) == l.charAt(1))
        {
            numOccurrences = numOccurrences + 1;
        }
    }
    return numOccurrences;
}

public class TextAnalyser
{
    private int   letters;     // the total number of letters analysed 

    private int[] frequencies; // the number of occurrences of each letter of the alphabet (case-insensitive)

    // initialise the instance variables 
    public TextAnalyser()
    {
        this.letters = 0;
        this.frequencies = new int[26];
    }

    public TextAnalyser(String initial)
    {
        this.letters = 0;
        this.frequencies = new int [26];
        this.analyse(initial);
    }
}

1 个答案:

答案 0 :(得分:0)

如果能够帮到你,你想计算字符串中字符c的出现次数吗?如果是,则StringUtils具有您可以使用的内容

int count = StringUtils.countMatches(CharSequence myString, CharSequence subString);

计算子串出现在myString中的次数。 从Here

了解更多信息