-3

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);
    }
}

}
halfer
  • 19,824
  • 17
  • 99
  • 186
SeanleArch
  • 19
  • 2
  • 2
    What about the error message do you not understand? It sounds self explanatory. – mason May 07 '18 at 18:39
  • Sure does mason, i must be missing something pretty obvious i guess :( – SeanleArch May 07 '18 at 18:41
  • You still have not answered my question. What part of the error message do you not understand? – mason May 07 '18 at 18:43
  • Yeah it does point me to where it fails, this is a code ive got from a friend that im just trying to figure out how to get working. i thought that i could ask here to get some pointers! some guys gave me pointers so i will look into it! – SeanleArch May 07 '18 at 18:49

2 Answers2

3

The problem is:

sample1 = sample1.Substring(i, sample1.Length);

and the other similar method calls. The second parameter of Substring is the length (i.e. the number of characters to retrieve for the substring). Thus, if i is greater than 0, it should fail in this case because the method will try to retrieve characters that aren't in the string.

  • okay! i will look into this thanks. Sorry if its a stupid question trying to help a friend and at the same time learn :) – SeanleArch May 07 '18 at 18:50
  • @SeanleArch It's not a dumb question at all - various languages are extremely inconsistent about this. In fact, in Java the second argument *is* the exclusive end index, so the code in your question would work exactly as written in Java. – EJoshuaS - Stand with Ukraine May 07 '18 at 18:52
0

One of your loops is trying to access an element that doesn't exist. For example you have an array a ={1,2,3}, you are trying to access the fourth element, which doesn't exist.

If you can't find out where exactly, there could be an issue, try using print statements inside your loops, displaying the counter (i) values. It will point out for which iteration, your code is failing.

Sammy
  • 47
  • 7