堆栈溢出异常错误(回文)C#

问题描述 投票:0回答:0

无法弄清楚这个溢出异常错误,我相信它与 bool 语句有关但不确定如何解决它。我上传了一个回文列表,该列表被读取并分配给字符串变量。

这个 stackoverflow 错误是否有任何问题?

使用系统;

命名空间回文 { 使用 System.IO;

public class Program
{
    public static void Main()
    {
        int c = 0;

        string[] l = File.ReadAllLines("UKACD17.TXT");
        for (int i = 0; i < l.Length; i++) //use for foreach loop can be faster and save a little more memory.
        {
            string ll = l[i];
            if (T(ll)) 
            {
                Console.WriteLine(ll);
                c++;
            }
        }

        Console.WriteLine("Found {0} palindromes.", c);
    }

    private static bool T(string s)
    {
        if (string.IsNullOrWhiteSpace(s)) return false; //Stops at white spaces, why is this returning a value from the parameter set up?
        return s.Length == 1 || (s[0] == s[s.Length - 1] && T(s.Substring(1, s.Length - 2))); 
    }


}

}

回文比较时出现错误:private static bool T(string s)。一种递归逻辑,比较第一个和最后一个字母,然后再次使用该逻辑来测试中间的字母。例如:Blob,B vs b(真),然后再次使用代码并测试 l vs o(假)。

我应该创建一个例外吗?或者有没有办法通过逻辑解决这个问题?

c# recursion boolean stack-overflow palindrome
© www.soinside.com 2019 - 2024. All rights reserved.