This code is supposed to take two samples one original and one new, then determine the length of the smallest single consecutive piece that has been inserted into the first sequence.
When trying some samples I get the following error message:
System.ArgumentOutOfRangeException: 'Index and length must refer to a place within the string. Parameter name: length
Here is the code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetSample());
Console.ReadKey();
}
public static int GetSample()
{
string sample1 = Console.ReadLine();
string sample2 = Console.ReadLine();
if (sample1 == sample2) return 0;
if (sample1.Length >= sample2.Length)
{
for (int i = 0; i < sample2.Length; i++)
{
if (!(sample1[i] == sample2[i]))
{
sample1 = sample1.Substring(i, sample1.Length);
sample2 = sample2.Substring(i, sample2.Length);
break;
}
}
int var = sample1.Length - sample2.Length;
for (int i = sample2.Length - 1; i >= 0; i--)
{
if (sample2[i] == sample1[i + var])
sample2 = trimlast(sample2);
}
}
else
{
for (int i = 0; i < sample1.Length; i++)
{
if (!(sample1[i] == sample2[i]))
{
sample1 = sample1.Substring(i, sample1.Length);
sample2 = sample2.Substring(i, sample2.Length);
break;
}
}
int var = sample2.Length - sample1.Length;
for (int i = sample1.Length - 1; i >= 0; i--)
{
if (sample2[i + var] == sample1[i])
sample2 = trimlast(sample2);
}
}
return sample2.Length;
}
public static string trimlast(string str)
{
return str.Substring(0, str.Length - 1);
}
}
}