如何计算c#上句子中的特定字母?

如何计算c#上句子中的特定字母?,c#,C#,一种程序,它接收字符串上的一个句子,然后接收字符上的一个字母,然后计算我们接收到的同一个字母的数目(解决方案必须是c#上的递归) 如果你想让它递归,你应该考虑如何分割它,并以同样的方式处理问题的每一部分,还有一些极端情况 我已经在下面编写了递归函数,我将尽力解释。我猜这是家庭作业,所以请不要跳过解释 说明: static int CountChar (char c, string sentence) { if (sentence.Length == 0) retu

一种程序,它接收字符串上的一个句子,然后接收字符上的一个字母,然后计算我们接收到的同一个字母的数目(解决方案必须是c#上的递归)


如果你想让它递归,你应该考虑如何分割它,并以同样的方式处理问题的每一部分,还有一些极端情况

我已经在下面编写了递归函数,我将尽力解释。我猜这是家庭作业,所以请不要跳过解释

说明:

    static int CountChar (char c, string sentence)
    {
        if (sentence.Length == 0) return 0;

        var firstLetter = sentence[0];
        var restOfSentence = sentence.Substring(1);
        int count = firstLetter.Equals(c) ? 1 : 0;
        return count + CountChar(c, restOfSentence);
    }
你可以在句子中找到你的角色(c)。 每个步骤的解决方案都是相同的,但角落的大小写有一个空句子,其中您的角色没有出现

在每个递归步骤中,您都希望分割问题。最简单的解决办法是将句子分成第一个字符和句子的其余部分。因此,解决方案将是以下各项的总和: a) 第一个字符是否匹配,以及 b) 你的角色出现在句子的其余部分

递归函数:

    static int CountChar (char c, string sentence)
    {
        if (sentence.Length == 0) return 0;

        var firstLetter = sentence[0];
        var restOfSentence = sentence.Substring(1);
        int count = firstLetter.Equals(c) ? 1 : 0;
        return count + CountChar(c, restOfSentence);
    }

如果你想让它递归,你应该考虑如何分割它,并以同样的方式处理问题的每一部分,还有一些极端情况

我已经在下面编写了递归函数,我将尽力解释。我猜这是家庭作业,所以请不要跳过解释

说明:

    static int CountChar (char c, string sentence)
    {
        if (sentence.Length == 0) return 0;

        var firstLetter = sentence[0];
        var restOfSentence = sentence.Substring(1);
        int count = firstLetter.Equals(c) ? 1 : 0;
        return count + CountChar(c, restOfSentence);
    }
你可以在句子中找到你的角色(c)。 每个步骤的解决方案都是相同的,但角落的大小写有一个空句子,其中您的角色没有出现

在每个递归步骤中,您都希望分割问题。最简单的解决办法是将句子分成第一个字符和句子的其余部分。因此,解决方案将是以下各项的总和: a) 第一个字符是否匹配,以及 b) 你的角色出现在句子的其余部分

递归函数:

    static int CountChar (char c, string sentence)
    {
        if (sentence.Length == 0) return 0;

        var firstLetter = sentence[0];
        var restOfSentence = sentence.Substring(1);
        int count = firstLetter.Equals(c) ? 1 : 0;
        return count + CountChar(c, restOfSentence);
    }

下面是一个通过递归计算文本中字符计数的示例:

    static void Main(string[] args)
    {
        Console.WriteLine(CountChar("asdasdasdasd",'a'));
        Console.WriteLine(CountChar("asdasdasdasdbb", 'b'));
    }

    public static int CountChar(string text, char character)
    {
        if(text.Length==1)
        {
            if (text[0] == character)
                return 1;
            else
                return 0;
        }
        else
        {
            return (text[0] == character ? 1 : 0) + CountChar(text.Substring(1), character);
        }

    }

下面是一个通过递归计算文本中字符计数的示例:

    static void Main(string[] args)
    {
        Console.WriteLine(CountChar("asdasdasdasd",'a'));
        Console.WriteLine(CountChar("asdasdasdasdbb", 'b'));
    }

    public static int CountChar(string text, char character)
    {
        if(text.Length==1)
        {
            if (text[0] == character)
                return 1;
            else
                return 0;
        }
        else
        {
            return (text[0] == character ? 1 : 0) + CountChar(text.Substring(1), character);
        }

    }

那么这个代码面临什么问题呢?那么这个代码面临什么问题呢?