1

I have a program that returns the distance from an ASCII table of two mismatched characters that are at the same position in two strings. I would like to shorten my program, but in such a way that it returns the same result as before.(The program cannot return negative values, so it checks in each If instruction which number has the greater value and strings always have the same length) Now program throws an error every time when comparing: "System.IndexOutOfRangeException: 'Index was outside the bounds of the array." but it displays the correct result ..

Code

    public static void incompatible(string a, string b)

      {
        char[] characters1 = new char[a.Length];
        char[] characters2 = new char[b.Length];

        for (int i = 0; i < a.Length; i++)
        {
            characters1 [i] = a[i];

        }
        for (int i = 0; i < b.Length; i++)
        {
            characters2 [i] = b[i];

        }



        if (a.Length >= 0 || b.Length >= 0)
        {
            if (characters1 [0] != characters2 [0])
            {
                if (characters1 [0] > characters2 [0])
                {
                    Console.WriteLine(characters1 [0] - characters2 [0]);
                }
                else if (characters1 [0] < characters2 [0])
                {
                    Console.WriteLine(characters2 [0] - characters1 [0]);
                }
            }
        }

        if (a.Length >= 1 || b.Length >= 1)
        {
            if (characters1 [1] != characters2 [1])
            {
                if (characters1 [1] > characters2 [1])
                {
                    Console.WriteLine(characters1 [1] - characters2 [1]);
                }
                else if (characters1 [1] < characters2 [1])
                {
                    Console.WriteLine(characters2 [1] - characters1 [1]);
                }
            }
        }

        if (a.Length >= 2 || b.Length >= 2)
        {
            if (characters1 [2] != characters2 [2])
            {
                if (characters1 [2] > characters2 [2])
                {
                    Console.WriteLine(characters1 [2] - characters2 [2]);
                }
                else if (characters1 [2] < characters2 [2])
                {
                    Console.WriteLine(characters2 [2] - characters1 [2]);
                }
            }
        }

        if (a.Length >= 3 || b.Length >= 3)
        {
            if (characters1 [3] != characters2 [3])
            {
                if (characters1 [3] > characters2 [3])
                {
                    Console.WriteLine(characters1 [3] - characters2 [3]);
                }
                else if (characters1 [3] < characters2 [3])
                {
                    Console.WriteLine(characters2 [3] - characters1 [3]);
                }
            }
        }

I have now fourteen If instructions like those above.

Yezre
  • 49
  • 6

2 Answers2

5

1.You don't need to use extra char arrays, and you can use a[index], b[index].

2.Math.Abs() will return the positive difference, so instead of lines like:

if (characters1 [2] > characters2 [2])
{
      Console.WriteLine(characters1 [2] - characters2 [2]);
}
else if (characters1 [2] < characters2 [2])
{
      Console.WriteLine(characters2 [2] - characters1 [2]);
}

you can do:

Console.WriteLine(Math.Abs(characters1 [0] - characters2 [0]));

so about looping, you whole code can become like:

  public static void incompatible(string a, string b)
  {
       for(int i=0; i<Math.Min(a.Length,b.Length);i++)
            if(a[i] != b[i])
                Console.WriteLine(Math.Abs(characters1 [i] - characters2 [i]));
  }
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
1

There is no need to copy the characters into the characters1 and characters2 variables. Strings do have an indexer through which you can access all their characters.

First get the minimum length of the two strings. We can compare only up to the length of the shorter string.

int length = Math.Min(a.Length, b.Length);

Now loop over the characters using a for loop

for (int i = 0; i < length; i++) {
    ... more to come
}

In the loop you can compare characters at the same position by using the index i. Take the absolute value of the difference of their character code. This eliminates the negative signs.

int diff = Math.Abs(a[i] - b[i]);

Note that this is the only time we are accessing characters. Each one is accessed exactly once. Your code accesses each one up to 5 times (when copying, when comparing !=, >, < and finally in the Writeline.

If the difference is not 0 write the result to the console

if (diff != 0) {
    Console.WriteLine(diff);
}

And now all together

public static void incompatible(string a, string b)
{
    int length = Math.Min(a.Length, b.Length);
    for (int i = 0; i < length; i++) {
        int diff = Math.Abs(a[i] - b[i]);
        if (diff != 0) {
            Console.WriteLine(diff);
        }
    }
}

That's it!

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188