Having trouble figuring out this overflow exception error, I believe it has to do with the bool statement but unsure how to get around it. I upload a list of palindromes and the list is read and assigned to string variables.
Is there anyway around this stackoverflow error?
using System;
namespace Palindrome
{
using 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)));
}
}
}
The error happens during the palindrome comparison: private static bool T(string s). A recursive logic that compares the first and last letters and then uses the logic again to test the letters in between. Ex: Blob, B vs b (true), then uses the code again and tests l vs o (false).
Should I create an exception? Or is there a way around this through the logic?